home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / dired.el.z / dired.el
Lisp/Scheme  |  1998-10-27  |  95KB  |  2,465 lines

  1. ;;; dired.el --- directory-browsing commands
  2.  
  3. ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This is a major mode for directory browsing and editing.  It is
  28. ;; documented in the Emacs manual.
  29.  
  30. ;; Rewritten in 1990/1991 to add tree features, file marking and
  31. ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
  32. ;; Finished up by rms in 1992.
  33.  
  34. ;;; Code:
  35.  
  36. ;;; Customizable variables
  37.  
  38. ;;;###autoload
  39. (defvar dired-listing-switches "-al"
  40.   "*Switches passed to `ls' for dired.  MUST contain the `l' option.
  41. May contain all other options that don't contradict `-l';
  42. may contain even `F', `b', `i' and `s'.  See also the variable
  43. `dired-ls-F-marks-symlinks' concerning the `F' switch.")
  44.  
  45. ; Don't use absolute paths as /bin should be in any PATH and people
  46. ; may prefer /usr/local/gnu/bin or whatever.  However, chown is
  47. ; usually not in PATH.
  48.  
  49. ;;;###autoload
  50. (defvar dired-chown-program
  51.   (if (memq system-type '(hpux dgux usg-unix-v irix linux lignux))
  52.       "chown"
  53.     (if (file-exists-p "/usr/sbin/chown")
  54.     "/usr/sbin/chown"
  55.       "/etc/chown"))
  56.   "Name of chown command (usually `chown' or `/etc/chown').")
  57.  
  58. (defvar dired-chmod-program
  59.   (if (eq system-type 'windows-nt)
  60.       "chmode" "chmod")
  61.   "Name of chmod command (usually `chmod' or `chmode').")
  62.  
  63. ;;;###autoload
  64. (defvar dired-ls-F-marks-symlinks nil
  65.   "*Informs dired about how `ls -lF' marks symbolic links.
  66. Set this to t if `ls' (or whatever program is specified by
  67. `insert-directory-program') with `-lF' marks the symbolic link
  68. itself with a trailing @ (usually the case under Ultrix).
  69.  
  70. Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
  71. nil (the default), if it gives `bar@ -> foo', set it to t.
  72.  
  73. Dired checks if there is really a @ appended.  Thus, if you have a
  74. marking `ls' program on one host and a non-marking on another host, and
  75. don't care about symbolic links which really end in a @, you can
  76. always set this variable to t.")
  77.  
  78. ;;;###autoload
  79. (defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
  80.   "*Regexp of files to skip when finding first file of a directory.
  81. A value of nil means move to the subdir line.
  82. A value of t means move to first file.")
  83.  
  84. ;;;###autoload
  85. (defvar dired-keep-marker-rename t
  86.   ;; Use t as default so that moved files "take their markers with them".
  87.   "*Controls marking of renamed files.
  88. If t, files keep their previous marks when they are renamed.
  89. If a character, renamed files (whether previously marked or not)
  90. are afterward marked with that character.")
  91.  
  92. ;;;###autoload
  93. (defvar dired-keep-marker-copy ?C
  94.   "*Controls marking of copied files.
  95. If t, copied files are marked if and as the corresponding original files were.
  96. If a character, copied files are unconditionally marked with that character.")
  97.  
  98. ;;;###autoload
  99. (defvar dired-keep-marker-hardlink ?H
  100.   "*Controls marking of newly made hard links.
  101. If t, they are marked if and as the files linked to were marked.
  102. If a character, new links are unconditionally marked with that character.")
  103.  
  104. ;;;###autoload
  105. (defvar dired-keep-marker-symlink ?Y
  106.   "*Controls marking of newly made symbolic links.
  107. If t, they are marked if and as the files linked to were marked.
  108. If a character, new links are unconditionally marked with that character.")
  109.  
  110. ;;;###autoload
  111. (defvar dired-dwim-target nil
  112.   "*If non-nil, dired tries to guess a default target directory.
  113. This means: if there is a dired buffer displayed in the next window,
  114. use its current subdir, instead of the current subdir of this dired buffer.
  115.  
  116. The target is used in the prompt for file copy, rename etc.")
  117.  
  118. ;;;###autoload
  119. (defvar dired-copy-preserve-time t
  120.   "*If non-nil, Dired preserves the last-modified time in a file copy.
  121. \(This works on only some systems.)")
  122.  
  123. (defvar dired-font-lock-keywords
  124.   '(;; Put directory headers in italics.
  125.     ("^  \\(/.+\\)" 1 font-lock-type-face)
  126.     ;; Put symlinks in bold italics.
  127.     ("\\([^ ]+\\) -> [^ ]+$" . font-lock-function-name-face)
  128.     ;; Put marks in bold.
  129.     ("^[^ ]" . font-lock-reference-face)
  130.     ;; Put files that are subdirectories in bold.
  131.     ("^..d.* \\([^ ]+\\)$" 1 font-lock-keyword-face))
  132.   "Additional expressions to highlight in Dired mode.")
  133.  
  134. ;;; Hook variables
  135.  
  136. (defvar dired-load-hook nil
  137.   "Run after loading dired.
  138. You can customize key bindings or load extensions with this.")
  139.  
  140. (defvar dired-mode-hook nil
  141.   "Run at the very end of dired-mode.")
  142.  
  143. (defvar dired-before-readin-hook nil
  144.   "This hook is run before a dired buffer is read in (created or reverted).")
  145.  
  146. (defvar dired-after-readin-hook nil
  147.   "Hook run after each time a file or directory is read by Dired.
  148. After each listing of a file or directory, this hook is run
  149. with the buffer narrowed to the listing.")
  150. ;; Note this can't simply be run inside function `dired-ls' as the hook
  151. ;; functions probably depend on the dired-subdir-alist to be OK.
  152.  
  153. ;;; Internal variables
  154.  
  155. (defvar dired-marker-char ?*        ; the answer is 42
  156.   ;; so that you can write things like
  157.   ;; (let ((dired-marker-char ?X))
  158.   ;;    ;; great code using X markers ...
  159.   ;;    )
  160.   ;; For example, commands operating on two sets of files, A and B.
  161.   ;; Or marking files with digits 0-9.  This could implicate
  162.   ;; concentric sets or an order for the marked files.
  163.   ;; The code depends on dynamic scoping on the marker char.
  164.   "In Dired, the current mark character.
  165. This is what the `do' commands look for and what the `mark' commands store.")
  166.  
  167. (defvar dired-del-marker ?D
  168.   "Character used to flag files for deletion.")
  169.  
  170. (defvar dired-shrink-to-fit
  171.   t
  172. ;; I see no reason ever to make this nil -- rms.
  173. ;;  (> baud-rate search-slow-speed)
  174.   "Non-nil means Dired shrinks the display buffer to fit the marked files.")
  175.  
  176. (defvar dired-flagging-regexp nil);; Last regexp used to flag files.
  177.  
  178. (defvar dired-file-version-alist)
  179.  
  180. (defvar dired-directory nil
  181.   "The directory name or shell wildcard that was used as argument to `ls'.
  182. Local to each dired buffer.  May be a list, in which case the car is the
  183. directory name and the cdr is the actual files to list.")
  184.  
  185. (defvar dired-actual-switches nil
  186.   "The value of `dired-listing-switches' used to make this buffer's text.")
  187.  
  188. (defvar dired-re-inode-size "[0-9 \t]*"
  189.   "Regexp for optional initial inode and file size as made by `ls -i -s'.")
  190.  
  191. ;; These regexps must be tested at beginning-of-line, but are also
  192. ;; used to search for next matches, so neither omitting "^" nor
  193. ;; replacing "^" by "\n" (to make it slightly faster) will work.
  194.  
  195. (defvar dired-re-mark "^[^ \n]")
  196. ;; "Regexp matching a marked line.
  197. ;; Important: the match ends just after the marker."
  198. (defvar dired-re-maybe-mark "^. ")
  199. (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
  200. (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
  201. (defvar dired-re-exe;; match ls permission string of an executable file
  202.   (mapconcat (function
  203.           (lambda (x)
  204.         (concat dired-re-maybe-mark dired-re-inode-size x)))
  205.          '("-[-r][-w][xs][-r][-w].[-r][-w]."
  206.            "-[-r][-w].[-r][-w][xs][-r][-w]."
  207.            "-[-r][-w].[-r][-w].[-r][-w][xst]")
  208.          "\\|"))
  209. (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
  210. (defvar dired-re-dot "^.* \\.\\.?$")
  211.  
  212. ;; The subdirectory names in this list are expanded.
  213. (defvar dired-subdir-alist nil
  214.   "Association list of subdirectories and their buffer positions.
  215. Each subdirectory has an element: (DIRNAME . STARTMARKER).
  216. The order of elements is the reverse of the order in the buffer.
  217. In simple cases, this list contains one element.")
  218.  
  219. (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
  220.   "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
  221. Subexpression 1 is the subdirectory proper, no trailing colon.
  222. The match starts at the beginning of the line and ends after the end
  223. of the line (\\n or \\r).
  224. Subexpression 2 must end right before the \\n or \\r.")
  225.  
  226.  
  227. ;;; Macros must be defined before they are used, for the byte compiler.
  228.  
  229. ;; Mark all files for which CONDITION evals to non-nil.
  230. ;; CONDITION is evaluated on each line, with point at beginning of line.
  231. ;; MSG is a noun phrase for the type of files being marked.
  232. ;; It should end with a noun that can be pluralized by adding `s'.
  233. ;; Return value is the number of files marked, or nil if none were marked.
  234. (defmacro dired-mark-if (predicate msg)
  235.   (` (let (buffer-read-only count)
  236.        (save-excursion
  237.      (setq count 0)
  238.      (if (, msg) (message "Marking %ss..." (, msg)))
  239.      (goto-char (point-min))
  240.      (while (not (eobp))
  241.        (if (, predicate)
  242.            (progn
  243.          (delete-char 1)
  244.          (insert dired-marker-char)
  245.          (setq count (1+ count))))
  246.        (forward-line 1))
  247.      (if (, msg) (message "%s %s%s %s%s."
  248.               count
  249.               (, msg)
  250.               (dired-plural-s count)
  251.               (if (eq dired-marker-char ?\040) "un" "")
  252.               (if (eq dired-marker-char dired-del-marker)
  253.                   "flagged" "marked"))))
  254.        (and (> count 0) count))))
  255.  
  256. (defmacro dired-map-over-marks (body arg &optional show-progress)
  257. ;;  "Macro: Perform BODY with point somewhere on each marked line
  258. ;;and return a list of BODY's results.
  259. ;;If no marked file could be found, execute BODY on the current line.
  260. ;;  If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
  261. ;;  files instead of the marked files.
  262. ;;  In that case point is dragged along.  This is so that commands on
  263. ;;  the next ARG (instead of the marked) files can be chained easily.
  264. ;;  If ARG is otherwise non-nil, use current file instead.
  265. ;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
  266. ;;   redisplay the dired buffer after each file is processed.
  267. ;;No guarantee is made about the position on the marked line.
  268. ;;  BODY must ensure this itself if it depends on this.
  269. ;;Search starts at the beginning of the buffer, thus the car of the list
  270. ;;  corresponds to the line nearest to the buffer's bottom.  This
  271. ;;  is also true for (positive and negative) integer values of ARG.
  272. ;;BODY should not be too long as it is expanded four times."
  273. ;;
  274. ;;Warning: BODY must not add new lines before point - this may cause an
  275. ;;endless loop.
  276. ;;This warning should not apply any longer, sk  2-Sep-1991 14:10.
  277.   (` (prog1
  278.      (let (buffer-read-only case-fold-search found results)
  279.        (if (, arg)
  280.            (if (integerp (, arg))
  281.            (progn;; no save-excursion, want to move point.
  282.              (dired-repeat-over-lines
  283.               (, arg)
  284.               (function (lambda ()
  285.                   (if (, show-progress) (sit-for 0))
  286.                   (setq results (cons (, body) results)))))
  287.              (if (< (, arg) 0)
  288.              (nreverse results)
  289.                results))
  290.          ;; non-nil, non-integer ARG means use current file:
  291.          (list (, body)))
  292.          (let ((regexp (dired-marker-regexp)) next-position)
  293.            (save-excursion
  294.          (goto-char (point-min))
  295.          ;; remember position of next marked file before BODY
  296.          ;; can insert lines before the just found file,
  297.          ;; confusing us by finding the same marked file again
  298.          ;; and again and...
  299.          (setq next-position (and (re-search-forward regexp nil t)
  300.                       (point-marker))
  301.                found (not (null next-position)))
  302.          (while next-position
  303.            (goto-char next-position)
  304.            (if (, show-progress) (sit-for 0))
  305.            (setq results (cons (, body) results))
  306.            ;; move after last match
  307.            (goto-char next-position)
  308.            (forward-line 1)
  309.            (set-marker next-position nil)
  310.            (setq next-position (and (re-search-forward regexp nil t)
  311.                         (point-marker)))))
  312.            (if found
  313.            results
  314.          (list (, body))))))
  315.        ;; save-excursion loses, again
  316.        (dired-move-to-filename))))
  317.  
  318. (defun dired-get-marked-files (&optional localp arg)
  319.   "Return the marked files' names as list of strings.
  320. The list is in the same order as the buffer, that is, the car is the
  321.   first marked file.
  322. Values returned are normally absolute pathnames.
  323. Optional arg LOCALP as in `dired-get-filename'.
  324. Optional second argument ARG forces to use other files.  If ARG is an
  325.   integer, use the next ARG files.  If ARG is otherwise non-nil, use
  326.   current file.  Usually ARG comes from the current prefix arg."
  327.   (save-excursion
  328.     (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
  329.  
  330.  
  331. ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
  332. ;; other special applications.
  333.  
  334. ;; The dired command
  335.  
  336. (defun dired-read-dir-and-switches (str)
  337.   ;; For use in interactive.
  338.   (reverse (list
  339.         (if current-prefix-arg
  340.         (read-string "Dired listing switches: "
  341.                  dired-listing-switches))
  342.         (read-file-name (format "Dired %s(directory): " str)
  343.                 nil default-directory nil))))
  344.  
  345. ;;;###autoload (define-key ctl-x-map "d" 'dired)
  346. ;;;###autoload
  347. (defun dired (dirname &optional switches)
  348.   "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
  349. Optional second argument SWITCHES specifies the `ls' options used.
  350. \(Interactively, use a prefix argument to be able to specify SWITCHES.)
  351. Dired displays a list of files in DIRNAME (which may also have
  352. shell wildcards appended to select certain files).  If DIRNAME is a cons,
  353. its first element is taken as the directory name and the rest as an explicit
  354. list of files to make directory entries for.
  355. \\<dired-mode-map>\
  356. You can move around in it with the usual commands.
  357. You can flag files for deletion with \\[dired-flag-file-deletion] and then
  358. delete them by typing \\[dired-do-flagged-delete].
  359. Type \\[describe-mode] after entering dired for more info.
  360.  
  361. If DIRNAME is already in a dired buffer, that buffer is used without refresh."
  362.   ;; Cannot use (interactive "D") because of wildcards.
  363.   (interactive (dired-read-dir-and-switches ""))
  364.   (switch-to-buffer (dired-noselect dirname switches)))
  365.  
  366. ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
  367. ;;;###autoload
  368. (defun dired-other-window (dirname &optional switches)
  369.   "\"Edit\" directory DIRNAME.  Like `dired' but selects in another window."
  370.   (interactive (dired-read-dir-and-switches "in other window "))
  371.   (switch-to-buffer-other-window (dired-noselect dirname switches)))
  372.  
  373. ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
  374. ;;;###autoload
  375. (defun dired-other-frame (dirname &optional switches)
  376.   "\"Edit\" directory DIRNAME.  Like `dired' but makes a new frame."
  377.   (interactive (dired-read-dir-and-switches "in other frame "))
  378.   (switch-to-buffer-other-frame (dired-noselect dirname switches)))
  379.  
  380. ;;;###autoload
  381. (defun dired-noselect (dir-or-list &optional switches)
  382.   "Like `dired' but returns the dired buffer as value, does not select it."
  383.   (or dir-or-list (setq dir-or-list default-directory))
  384.   ;; This loses the distinction between "/foo/*/" and "/foo/*" that
  385.   ;; some shells make:
  386.   (let (dirname)
  387.     (if (consp dir-or-list)
  388.     (setq dirname (car dir-or-list))
  389.       (setq dirname dir-or-list))
  390.     (setq dirname (abbreviate-file-name
  391.            (expand-file-name (directory-file-name dirname))))
  392.     (if find-file-visit-truename
  393.     (setq dirname (file-truename dirname)))
  394.     (if (file-directory-p dirname)
  395.     (setq dirname (file-name-as-directory dirname)))
  396.     (if (consp dir-or-list)
  397.     (setq dir-or-list (cons dirname (cdr dir-or-list)))
  398.       (setq dir-or-list dirname))
  399.     (dired-internal-noselect dir-or-list switches)))
  400.  
  401. ;; Separate function from dired-noselect for the sake of dired-vms.el.
  402. (defun dired-internal-noselect (dir-or-list &optional switches mode)
  403.   ;; If there is an existing dired buffer for DIRNAME, just leave
  404.   ;; buffer as it is (don't even call dired-revert).
  405.   ;; This saves time especially for deep trees or with ange-ftp.
  406.   ;; The user can type `g'easily, and it is more consistent with find-file.
  407.   ;; But if SWITCHES are given they are probably different from the
  408.   ;; buffer's old value, so call dired-sort-other, which does
  409.   ;; revert the buffer.
  410.   ;; A pity we can't possibly do "Directory has changed - refresh? "
  411.   ;; like find-file does.
  412.   ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
  413.   ;; see there.
  414.   (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
  415.      ;; The following line used to use dir-or-list.
  416.      ;; That never found an existing buffer, in the case
  417.      ;; where it is a list.
  418.      (buffer (dired-find-buffer-nocreate dirname mode))
  419.      ;; note that buffer already is in dired-mode, if found
  420.      (new-buffer-p (not buffer))
  421.      (old-buf (current-buffer)))
  422.     (or buffer
  423.     (let ((default-major-mode 'fundamental-mode))
  424.       ;; We don't want default-major-mode to run hooks and set auto-fill
  425.       ;; or whatever, now that dired-mode does not
  426.       ;; kill-all-local-variables any longer.
  427.       (setq buffer (create-file-buffer (directory-file-name dirname)))))
  428.     (set-buffer buffer)
  429.     (if (not new-buffer-p)     ; existing buffer ...
  430.     (cond (switches        ; ... but new switches     
  431.            ;; file list may have changed
  432.            (if (consp dir-or-list) 
  433.            (setq dired-directory dir-or-list))
  434.            ;; this calls dired-revert
  435.            (dired-sort-other switches))  
  436.           ;; If directory has changed on disk, offer to revert.
  437.           ((if (let ((attributes (file-attributes dirname))
  438.              (modtime (visited-file-modtime)))
  439.              (or (eq modtime 0)
  440.              (not (eq (car attributes) t))
  441.              (and (= (car (nth 5 attributes)) (car modtime))
  442.                   (= (nth 1 (nth 5 attributes)) (cdr modtime)))))
  443.            nil
  444.          (message "%s"
  445.               (substitute-command-keys
  446.                "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
  447.       ;; Else a new buffer
  448.       (setq default-directory
  449.         (if (file-directory-p dirname)
  450.         dirname
  451.           (file-name-directory dirname)))
  452.       (or switches (setq switches dired-listing-switches))
  453.       (dired-mode dirname switches)
  454.       (if mode (funcall mode))
  455.       ;; default-directory and dired-actual-switches are set now
  456.       ;; (buffer-local), so we can call dired-readin:
  457.       (let ((failed t))
  458.     (unwind-protect
  459.         (progn (dired-readin dir-or-list buffer)
  460.            (setq failed nil))
  461.       ;; dired-readin can fail if parent directories are inaccessible.
  462.       ;; Don't leave an empty buffer around in that case.
  463.       (if failed (kill-buffer buffer))))
  464.       ;; No need to narrow since the whole buffer contains just
  465.       ;; dired-readin's output, nothing else.  The hook can
  466.       ;; successfully use dired functions (e.g. dired-get-filename)
  467.       ;; as the subdir-alist has been built in dired-readin.
  468.       (run-hooks 'dired-after-readin-hook)
  469.       (goto-char (point-min))
  470.       (dired-initial-position dirname))
  471.     (set-buffer old-buf)
  472.     buffer))
  473.  
  474. (defun dired-find-buffer-nocreate (dirname &optional mode)
  475.   ;; This differs from dired-buffers-for-dir in that it does not consider
  476.   ;; subdirs of default-directory and searches for the first match only.
  477.   ;; Also, the major mode must be MODE.
  478.   (let (found (blist dired-buffers))    ; was (buffer-list)
  479.     (or mode (setq mode 'dired-mode))
  480.     (while blist
  481.       (if (null (buffer-name (cdr (car blist))))
  482.       (setq blist (cdr blist))
  483.     (save-excursion
  484.       (set-buffer (cdr (car blist)))
  485.       (if (and (eq major-mode mode)
  486.            (if (consp dired-directory)
  487.                (equal (car dired-directory) dirname)
  488.              (equal dired-directory dirname)))
  489.           (setq found (cdr (car blist))
  490.             blist nil)
  491.         (setq blist (cdr blist))))))
  492.     found))
  493.  
  494.  
  495. ;; Read in a new dired buffer
  496.  
  497. ;; dired-readin differs from dired-insert-subdir in that it accepts
  498. ;; wildcards, erases the buffer, and builds the subdir-alist anew
  499. ;; (including making it buffer-local and clearing it first).
  500. (defun dired-readin (dir-or-list buffer)
  501.   ;; default-directory and dired-actual-switches must be buffer-local
  502.   ;; and initialized by now.
  503.   ;; Thus we can test (equal default-directory dirname) instead of
  504.   ;; (file-directory-p dirname) and save a filesystem transaction.
  505.   ;; Also, we can run this hook which may want to modify the switches
  506.   ;; based on default-directory, e.g. with ange-ftp to a SysV host
  507.   ;; where ls won't understand -Al switches.
  508.   (let (dirname)
  509.     (if (consp dir-or-list)
  510.     (setq dirname (car dir-or-list))
  511.       (setq dirname dir-or-list))
  512.     (setq dirname (expand-file-name dirname))
  513.     (if (consp dir-or-list)
  514.     (setq dir-or-list (cons dirname (cdr dir-or-list))))
  515.     (run-hooks 'dired-before-readin-hook)
  516.     (save-excursion
  517.       (message "Reading directory %s..." dirname)
  518.       (set-buffer buffer)
  519.       (let (buffer-read-only (failed t))
  520.     (widen)
  521.     (erase-buffer)
  522.     (dired-readin-insert dir-or-list)
  523.     (indent-rigidly (point-min) (point-max) 2)
  524.     ;; We need this to make the root dir have a header line as all
  525.     ;; other subdirs have:
  526.     (goto-char (point-min))
  527.     (dired-insert-headerline default-directory)
  528.     ;; can't run dired-after-readin-hook here, it may depend on the subdir
  529.     ;; alist to be OK.
  530.     )
  531.       (message "Reading directory %s...done" dirname)
  532.       ;; Must first make alist buffer local and set it to nil because
  533.       ;; dired-build-subdir-alist will call dired-clear-alist first
  534.       (set (make-local-variable 'dired-subdir-alist) nil)
  535.       (dired-build-subdir-alist)
  536.       (let ((attributes (file-attributes dirname)))
  537.     (if (eq (car attributes) t)
  538.         (set-visited-file-modtime (nth 5 attributes))))
  539.       (set-buffer-modified-p nil))))
  540.  
  541. ;; Subroutines of dired-readin
  542.  
  543. (defun dired-readin-insert (dir-or-list)
  544.   ;; Just insert listing for the passed-in directory or
  545.   ;; directory-and-file list, assuming a clean buffer.
  546.   (let (dirname)
  547.     (if (consp dir-or-list)
  548.     (setq dirname (car dir-or-list))
  549.       (setq dirname dir-or-list))
  550.     ;; Expand before comparing in case one or both have been abbreviated.
  551.     (if (and (equal (expand-file-name default-directory)
  552.             (expand-file-name dirname))
  553.          (not (consp dir-or-list)))
  554.     ;; If we are reading a whole single directory...
  555.     (dired-insert-directory dir-or-list dired-actual-switches nil t)
  556.       (if (not (file-readable-p
  557.         (directory-file-name (file-name-directory dirname))))
  558.       (error "Directory %s inaccessible or nonexistent" dirname)
  559.     ;; Else assume it contains wildcards,
  560.     ;; unless it is an explicit list of files.
  561.     (dired-insert-directory dir-or-list dired-actual-switches
  562.                 (not (listp dir-or-list)))
  563.     (save-excursion        ;; insert wildcard instead of total line:
  564.       (goto-char (point-min))
  565.       (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
  566.  
  567. (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
  568.   ;; Do the right thing whether dir-or-list is atomic or not.  If it is,
  569.   ;; inset all files listed in the cdr (the car is the passed-in directory
  570.   ;; list).
  571.   (let ((opoint (point))
  572.     (process-environment (copy-sequence process-environment))
  573.     end)
  574.     ;; This makes sure that month names come out in English
  575.     ;; so we can find the start of the file name.
  576.     ;; But if the user has customized the way of finding the file name,
  577.     ;; this is not necessary.
  578.     (if (and (equal dired-move-to-filename-regexp
  579.             dired-standard-move-to-filename-regexp)
  580.          ;; It also isn't necessary if we'd use the C locale anyway.
  581.          (not (equal (or (getenv "LC_ALL") (getenv "LC_TIME")
  582.                  (getenv "LANGUAGE") "C")
  583.              "C")))
  584.     (setq process-environment (cons "LC_ALL=C" process-environment)))
  585.     (if (consp dir-or-list)
  586.     ;; In this case, use the file names in the cdr
  587.     ;; exactly as originally given to dired-noselect.
  588.     (mapcar
  589.      (function (lambda (x) (insert-directory x switches wildcard full-p)))
  590.      (cdr dir-or-list))
  591.       ;; Expand the file name here because it may have been abbreviated
  592.       ;; in dired-noselect.
  593.       (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
  594.     ;; Quote certain characters, unless ls quoted them for us.
  595.     (if (not (string-match "b" dired-actual-switches))
  596.     (save-excursion
  597.       (setq end (point-marker))
  598.       (goto-char opoint)
  599.       (while (search-forward "\\" end t)
  600.         (replace-match "\\\\" nil t))
  601.       (goto-char opoint)
  602.       (while (search-forward "\^m" end t)
  603.         (replace-match "\\015" nil t))
  604.       (set-marker end nil)))
  605.     (dired-insert-set-properties opoint (point)))
  606.   (setq dired-directory dir-or-list))
  607.  
  608. ;; Make the file names highlight when the mouse is on them.
  609. (defun dired-insert-set-properties (beg end)
  610.   (save-excursion
  611.     (goto-char beg)
  612.     (while (< (point) end)
  613.       (condition-case nil
  614.       (if (dired-move-to-filename)
  615.           (put-text-property (point)
  616.                  (save-excursion
  617.                    (dired-move-to-end-of-filename)
  618.                    (point))
  619.                  'mouse-face 'highlight))
  620.     (error nil))
  621.       (forward-line 1))))
  622.  
  623. (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
  624.   ;; Insert DIR's headerline with no trailing slash, exactly like ls
  625.   ;; would, and put cursor where dired-build-subdir-alist puts subdir
  626.   ;; boundaries.
  627.   (save-excursion (insert "  " (directory-file-name dir) ":\n")))
  628.  
  629.  
  630. ;; Reverting a dired buffer
  631.  
  632. (defun dired-revert (&optional arg noconfirm)
  633.   ;; Reread the dired buffer.  Must also be called after
  634.   ;; dired-actual-switches have changed.
  635.   ;; Should not fail even on completely garbaged buffers.
  636.   ;; Preserves old cursor, marks/flags, hidden-p.
  637.   (widen)                ; just in case user narrowed
  638.   (let ((opoint (point))
  639.     (ofile (dired-get-filename nil t))
  640.     (mark-alist nil)        ; save marked files
  641.     (hidden-subdirs (dired-remember-hidden))
  642.     (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
  643.     (case-fold-search nil)        ; we check for upper case ls flags
  644.     buffer-read-only)
  645.     (goto-char (point-min))
  646.     (setq mark-alist;; only after dired-remember-hidden since this unhides:
  647.       (dired-remember-marks (point-min) (point-max)))
  648.     ;; treat top level dir extra (it may contain wildcards)
  649.     (dired-uncache
  650.      (if (consp dired-directory) (car dired-directory) dired-directory))
  651.     (dired-readin dired-directory (current-buffer))
  652.     (let ((dired-after-readin-hook nil))
  653.       ;; don't run that hook for each subdir...
  654.       (dired-insert-old-subdirs old-subdir-alist))
  655.     (dired-mark-remembered mark-alist)    ; mark files that were marked
  656.     ;; ... run the hook for the whole buffer, and only after markers
  657.     ;; have been reinserted (else omitting in dired-x would omit marked files)
  658.     (run-hooks 'dired-after-readin-hook)    ; no need to narrow
  659.     (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
  660.     (goto-char opoint))        ; was before
  661.     (dired-move-to-filename)
  662.     (save-excursion            ; hide subdirs that were hidden
  663.       (mapcar (function (lambda (dir)
  664.               (if (dired-goto-subdir dir)
  665.                   (dired-hide-subdir 1))))
  666.           hidden-subdirs)))
  667.   ;; outside of the let scope
  668. ;;; Might as well not override the user if the user changed this.
  669. ;;;  (setq buffer-read-only t)
  670.   )
  671.  
  672. ;; Subroutines of dired-revert
  673. ;; Some of these are also used when inserting subdirs.
  674.  
  675. (defun dired-remember-marks (beg end)
  676.   ;; Return alist of files and their marks, from BEG to END.
  677.   (if selective-display            ; must unhide to make this work.
  678.       (let (buffer-read-only)
  679.     (subst-char-in-region beg end ?\r ?\n)))
  680.   (let (fil chr alist)
  681.     (save-excursion
  682.       (goto-char beg)
  683.       (while (re-search-forward dired-re-mark end t)
  684.     (if (setq fil (dired-get-filename nil t))
  685.         (setq chr (preceding-char)
  686.           alist (cons (cons fil chr) alist)))))
  687.     alist))
  688.  
  689. ;; Mark all files remembered in ALIST.
  690. ;; Each element of ALIST looks like (FILE . MARKERCHAR).
  691. (defun dired-mark-remembered (alist)
  692.   (let (elt fil chr)
  693.     (while alist
  694.       (setq elt (car alist)
  695.         alist (cdr alist)
  696.         fil (car elt)
  697.         chr (cdr elt))
  698.       (if (dired-goto-file fil)
  699.       (save-excursion
  700.         (beginning-of-line)
  701.         (delete-char 1)
  702.         (insert chr))))))
  703.  
  704. ;; Return a list of names of subdirs currently hidden.
  705. (defun dired-remember-hidden ()
  706.   (let ((l dired-subdir-alist) dir pos result)
  707.     (while l
  708.       (setq dir (car (car l))
  709.         pos (cdr (car l))
  710.         l (cdr l))
  711.       (goto-char pos)
  712.       (skip-chars-forward "^\r\n")
  713.       (if (eq (following-char) ?\r)
  714.       (setq result (cons dir result))))
  715.     result))
  716.  
  717. ;; Try to insert all subdirs that were displayed before,
  718. ;; according to the former subdir alist OLD-SUBDIR-ALIST.
  719. (defun dired-insert-old-subdirs (old-subdir-alist)
  720.   (or (string-match "R" dired-actual-switches)
  721.       (let (elt dir)
  722.     (while old-subdir-alist
  723.       (setq elt (car old-subdir-alist)
  724.         old-subdir-alist (cdr old-subdir-alist)
  725.         dir (car elt))
  726.       (condition-case ()
  727.           (progn
  728.         (dired-uncache dir)
  729.         (dired-insert-subdir dir))
  730.         (error nil))))))
  731.  
  732. ;; Remove directory DIR from any directory cache.
  733. (defun dired-uncache (dir)
  734.   (let ((handler (find-file-name-handler dir 'dired-uncache)))
  735.     (if handler
  736.     (funcall handler 'dired-uncache dir))))
  737.  
  738. ;; dired mode key bindings and initialization
  739.  
  740. (defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
  741. (if dired-mode-map
  742.     nil
  743.   ;; This looks ugly when substitute-command-keys uses C-d instead d:
  744.   ;;  (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
  745.  
  746.   (let ((map (make-keymap)))
  747.     (suppress-keymap map)
  748.     (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
  749.     ;; Commands to mark or flag certain categories of files
  750.     (define-key map "#" 'dired-flag-auto-save-files)
  751.     (define-key map "*" 'dired-mark-executables)
  752.     (define-key map "." 'dired-clean-directory)
  753.     (define-key map "/" 'dired-mark-directories)
  754.     (define-key map "@" 'dired-mark-symlinks)
  755.     (define-key map "~" 'dired-flag-backup-files)
  756.     ;; Upper case keys (except !) for operating on the marked files
  757.     (define-key map "A" 'dired-do-search)
  758.     (define-key map "C" 'dired-do-copy)
  759.     (define-key map "B" 'dired-do-byte-compile)
  760.     (define-key map "D" 'dired-do-delete)
  761.     (define-key map "G" 'dired-do-chgrp)
  762.     (define-key map "H" 'dired-do-hardlink)
  763.     (define-key map "L" 'dired-do-load)
  764.     (define-key map "M" 'dired-do-chmod)
  765.     (define-key map "O" 'dired-do-chown)
  766.     (define-key map "P" 'dired-do-print)
  767.     (define-key map "Q" 'dired-do-query-replace)
  768.     (define-key map "R" 'dired-do-rename)
  769.     (define-key map "S" 'dired-do-symlink)
  770.     (define-key map "X" 'dired-do-shell-command)
  771.     (define-key map "Z" 'dired-do-compress)
  772.     (define-key map "!" 'dired-do-shell-command)
  773.     ;; Comparison commands
  774.     (define-key map "=" 'dired-diff)
  775.     (define-key map "\M-=" 'dired-backup-diff)
  776.     ;; Tree Dired commands
  777.     (define-key map "\M-\C-?" 'dired-unmark-all-files)
  778.     (define-key map "\M-\C-d" 'dired-tree-down)
  779.     (define-key map "\M-\C-u" 'dired-tree-up)
  780.     (define-key map "\M-\C-n" 'dired-next-subdir)
  781.     (define-key map "\M-\C-p" 'dired-prev-subdir)
  782.     ;; move to marked files
  783.     (define-key map "\M-{" 'dired-prev-marked-file)
  784.     (define-key map "\M-}" 'dired-next-marked-file)
  785.     ;; Make all regexp commands share a `%' prefix:
  786.     ;; We used to get to the submap via a symbol dired-regexp-prefix,
  787.     ;; but that seems to serve little purpose, and copy-keymap
  788.     ;; does a better job without it.
  789.     (define-key map "%" nil)
  790.     (define-key map "%u" 'dired-upcase)
  791.     (define-key map "%l" 'dired-downcase)
  792.     (define-key map "%d" 'dired-flag-files-regexp)
  793.     (define-key map "%m" 'dired-mark-files-regexp)
  794.     (define-key map "%r" 'dired-do-rename-regexp)
  795.     (define-key map "%C" 'dired-do-copy-regexp)
  796.     (define-key map "%H" 'dired-do-hardlink-regexp)
  797.     (define-key map "%R" 'dired-do-rename-regexp)
  798.     (define-key map "%S" 'dired-do-symlink-regexp)
  799.     ;; Lower keys for commands not operating on all the marked files
  800.     (define-key map "c" 'dired-change-marks)
  801.     (define-key map "d" 'dired-flag-file-deletion)
  802.     (define-key map "e" 'dired-find-file)
  803.     (define-key map "f" 'dired-find-file)
  804.     (define-key map "\C-m" 'dired-advertised-find-file)
  805.     (define-key map "g" 'revert-buffer)
  806.     (define-key map "h" 'describe-mode)
  807.     (define-key map "i" 'dired-maybe-insert-subdir)
  808.     (define-key map "k" 'dired-do-kill-lines)
  809.     (define-key map "l" 'dired-do-redisplay)
  810.     (define-key map "m" 'dired-mark)
  811.     (define-key map "n" 'dired-next-line)
  812.     (define-key map "o" 'dired-find-file-other-window)
  813.     (define-key map "\C-o" 'dired-display-file)
  814.     (define-key map "p" 'dired-previous-line)
  815.     (define-key map "q" 'dired-quit)
  816.     (define-key map "s" 'dired-sort-toggle-or-edit)
  817.     (define-key map "u" 'dired-unmark)
  818.     (define-key map "v" 'dired-view-file)
  819.     (define-key map "x" 'dired-do-flagged-delete)
  820.     (define-key map "+" 'dired-create-directory)
  821.     ;; moving
  822.     (define-key map "<" 'dired-prev-dirline)
  823.     (define-key map ">" 'dired-next-dirline)
  824.     (define-key map "^" 'dired-up-directory)
  825.     (define-key map " "  'dired-next-line)
  826.     (define-key map "\C-n" 'dired-next-line)
  827.     (define-key map "\C-p" 'dired-previous-line)
  828.     (define-key map [down] 'dired-next-line)
  829.     (define-key map [up] 'dired-previous-line)
  830.     ;; hiding
  831.     (define-key map "$" 'dired-hide-subdir)
  832.     (define-key map "\M-$" 'dired-hide-all)
  833.     ;; misc
  834.     (define-key map "?" 'dired-summary)
  835.     (define-key map "\177" 'dired-unmark-backward)
  836.     (define-key map "\C-_" 'dired-undo)
  837.     (define-key map "\C-xu" 'dired-undo)
  838.  
  839.     ;; Make menu bar items.
  840.  
  841.     ;; Get rid of the Edit menu bar item to save space.
  842.     (define-key map [menu-bar edit] 'undefined)
  843.  
  844.     (define-key map [menu-bar subdir]
  845.       (cons "Subdir" (make-sparse-keymap "Subdir")))
  846.  
  847.     (define-key map [menu-bar subdir hide-all]
  848.       '("Hide All" . dired-hide-all))
  849.     (define-key map [menu-bar subdir hide-subdir]
  850.       '("Hide Subdir" . dired-hide-subdir))
  851.     (define-key map [menu-bar subdir tree-down]
  852.       '("Tree Down" . dired-tree-down))
  853.     (define-key map [menu-bar subdir tree-up]
  854.       '("Tree Up" . dired-tree-up))
  855.     (define-key map [menu-bar subdir up]
  856.       '("Up Directory" . dired-up-directory))
  857.     (define-key map [menu-bar subdir prev-subdir]
  858.       '("Prev Subdir" . dired-prev-subdir))
  859.     (define-key map [menu-bar subdir next-subdir]
  860.       '("Next Subdir" . dired-next-subdir))
  861.     (define-key map [menu-bar subdir prev-dirline]
  862.       '("Prev Dirline" . dired-prev-dirline))
  863.     (define-key map [menu-bar subdir next-dirline]
  864.       '("Next Dirline" . dired-next-dirline))
  865.     (define-key map [menu-bar subdir insert]
  866.       '("Insert This Subdir" . dired-maybe-insert-subdir))
  867.  
  868.     (define-key map [menu-bar immediate]
  869.       (cons "Immediate" (make-sparse-keymap "Immediate")))
  870.  
  871.     (define-key map [menu-bar immediate backup-diff]
  872.       '("Compare with Backup" . dired-backup-diff))
  873.     (define-key map [menu-bar immediate diff]
  874.       '("Diff" . dired-diff))
  875.     (define-key map [menu-bar immediate view]
  876.       '("View This File" . dired-view-file))
  877.     (define-key map [menu-bar immediate display]
  878.       '("Display in Other Window" . dired-display-file))
  879.     (define-key map [menu-bar immediate find-file-other-window]
  880.       '("Find in Other Window" . dired-find-file-other-window))
  881.     (define-key map [menu-bar immediate find-file]
  882.       '("Find This File" . dired-find-file))
  883.     (define-key map [menu-bar immediate create-directory]
  884.       '("Create Directory..." . dired-create-directory))
  885.  
  886.     (define-key map [menu-bar regexp]
  887.       (cons "Regexp" (make-sparse-keymap "Regexp")))
  888.  
  889.     (define-key map [menu-bar regexp downcase]
  890.       '("Downcase" . dired-downcase))
  891.     (define-key map [menu-bar regexp upcase]
  892.       '("Upcase" . dired-upcase))
  893.     (define-key map [menu-bar regexp hardlink]
  894.       '("Hardlink..." . dired-do-hardlink-regexp))
  895.     (define-key map [menu-bar regexp symlink]
  896.       '("Symlink..." . dired-do-symlink-regexp))
  897.     (define-key map [menu-bar regexp rename]
  898.       '("Rename..." . dired-do-rename-regexp))
  899.     (define-key map [menu-bar regexp copy]
  900.       '("Copy..." . dired-do-copy-regexp))
  901.     (define-key map [menu-bar regexp flag]
  902.       '("Flag..." . dired-flag-files-regexp))
  903.     (define-key map [menu-bar regexp mark]
  904.       '("Mark..." . dired-mark-files-regexp))
  905.  
  906.     (define-key map [menu-bar mark]
  907.       (cons "Mark" (make-sparse-keymap "Mark")))
  908.  
  909.     (define-key map [menu-bar mark prev]
  910.       '("Previous Marked" . dired-prev-marked-file))
  911.     (define-key map [menu-bar mark next]
  912.       '("Next Marked" . dired-next-marked-file))
  913.     (define-key map [menu-bar mark marks]
  914.       '("Change Marks..." . dired-change-marks))
  915.     (define-key map [menu-bar mark unmark-all]
  916.       '("Unmark All" . dired-unmark-all-files-no-query))
  917.     (define-key map [menu-bar mark symlinks]
  918.       '("Mark Symlinks" . dired-mark-symlinks))
  919.     (define-key map [menu-bar mark directories]
  920.       '("Mark Directories" . dired-mark-directories))
  921.     (define-key map [menu-bar mark directory]
  922.       '("Mark Old Backups" . dired-clean-directory))
  923.     (define-key map [menu-bar mark executables]
  924.       '("Mark Executables" . dired-mark-executables))
  925.     (define-key map [menu-bar mark backup-files]
  926.       '("Flag Backup Files" . dired-flag-backup-files))
  927.     (define-key map [menu-bar mark auto-save-files]
  928.       '("Flag Auto-save Files" . dired-flag-auto-save-files))
  929.     (define-key map [menu-bar mark deletion]
  930.       '("Flag" . dired-flag-file-deletion))
  931.     (define-key map [menu-bar mark unmark]
  932.       '("Unmark" . dired-unmark))
  933.     (define-key map [menu-bar mark mark]
  934.       '("Mark" . dired-mark))
  935.  
  936.     (define-key map [menu-bar operate]
  937.       (cons "Operate" (make-sparse-keymap "Operate")))
  938.  
  939.     (define-key map [menu-bar operate query-replace]
  940.       '("Query Replace in Files..." . dired-do-query-replace))
  941.     (define-key map [menu-bar operate search]
  942.       '("Search Files..." . dired-do-search))
  943.     (define-key map [menu-bar operate chown]
  944.       '("Change Owner..." . dired-do-chown))
  945.     (define-key map [menu-bar operate chgrp]
  946.       '("Change Group..." . dired-do-chgrp))
  947.     (define-key map [menu-bar operate chmod]
  948.       '("Change Mode..." . dired-do-chmod))
  949.     (define-key map [menu-bar operate load]
  950.       '("Load" . dired-do-load))
  951.     (define-key map [menu-bar operate compile]
  952.       '("Byte-compile" . dired-do-byte-compile))
  953.     (define-key map [menu-bar operate compress]
  954.       '("Compress" . dired-do-compress))
  955.     (define-key map [menu-bar operate print]
  956.       '("Print" . dired-do-print))
  957.     (define-key map [menu-bar operate hardlink]
  958.       '("Hardlink to..." . dired-do-hardlink))
  959.     (define-key map [menu-bar operate symlink]
  960.       '("Symlink to..." . dired-do-symlink))
  961.     (define-key map [menu-bar operate command]
  962.       '("Shell Command..." . dired-do-shell-command))
  963.     (define-key map [menu-bar operate delete]
  964.       '("Delete" . dired-do-delete))
  965.     (define-key map [menu-bar operate rename]
  966.       '("Rename to..." . dired-do-rename))
  967.     (define-key map [menu-bar operate copy]
  968.       '("Copy to..." . dired-do-copy))
  969.  
  970.     (setq dired-mode-map map)))
  971.  
  972. ;; Dired mode is suitable only for specially formatted data.
  973. (put 'dired-mode 'mode-class 'special)
  974.  
  975. (defun dired-mode (&optional dirname switches)
  976.   "\
  977. Mode for \"editing\" directory listings.
  978. In dired, you are \"editing\" a list of the files in a directory and
  979.   \(optionally) its subdirectories, in the format of `ls -lR'.
  980.   Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
  981. \"Editing\" means that you can run shell commands on files, visit,
  982.   compress, load or byte-compile them, change their file attributes
  983.   and insert subdirectories into the same buffer.  You can \"mark\"
  984.   files for later commands or \"flag\" them for deletion, either file
  985.   by file or all files matching certain criteria.
  986. You can move using the usual cursor motion commands.\\<dired-mode-map>
  987. Letters no longer insert themselves.  Digits are prefix arguments.
  988. Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
  989. Type \\[dired-mark] to Mark a file or subdirectory for later commands.
  990.   Most commands operate on the marked files and use the current file
  991.   if no files are marked.  Use a numeric prefix argument to operate on
  992.   the next ARG (or previous -ARG if ARG<0) files, or just `1'
  993.   to operate on the current file only.  Prefix arguments override marks.
  994.   Mark-using commands display a list of failures afterwards.  Type \\[dired-summary]
  995.   to see why something went wrong.
  996. Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
  997. Type \\[dired-unmark-backward] to back up one line and unflag.
  998. Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
  999. Type \\[dired-advertised-find-file] to Find the current line's file
  1000.   (or dired it in another buffer, if it is a directory).
  1001. Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
  1002. Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
  1003. Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
  1004. Type \\[dired-do-copy] to Copy files.
  1005. Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
  1006. Type \\[revert-buffer] to read all currently expanded directories again.
  1007.   This retains all marks and hides subdirs again that were hidden before.
  1008. SPC and DEL can be used to move down and up by lines.
  1009.  
  1010. If dired ever gets confused, you can either type \\[revert-buffer] \
  1011. to read the
  1012. directories again, type \\[dired-do-redisplay] \
  1013. to relist a single or the marked files or a
  1014. subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
  1015. again for the directory tree.
  1016.  
  1017. Customization variables (rename this buffer and type \\[describe-variable] on each line
  1018. for more info):
  1019.  
  1020.   dired-listing-switches
  1021.   dired-trivial-filenames
  1022.   dired-shrink-to-fit
  1023.   dired-marker-char
  1024.   dired-del-marker
  1025.   dired-keep-marker-rename
  1026.   dired-keep-marker-copy
  1027.   dired-keep-marker-hardlink
  1028.   dired-keep-marker-symlink
  1029.  
  1030. Hooks (use \\[describe-variable] to see their documentation):
  1031.  
  1032.   dired-before-readin-hook
  1033.   dired-after-readin-hook
  1034.   dired-mode-hook
  1035.   dired-load-hook
  1036.  
  1037. Keybindings:
  1038. \\{dired-mode-map}"
  1039.   ;; Not to be called interactively (e.g. dired-directory will be set
  1040.   ;; to default-directory, which is wrong with wildcards).
  1041.   (kill-all-local-variables)
  1042.   (use-local-map dired-mode-map)
  1043.   (dired-advertise)            ; default-directory is already set
  1044.   (setq major-mode 'dired-mode
  1045.     mode-name "Dired"
  1046. ;;    case-fold-search nil
  1047.     buffer-read-only t
  1048.     selective-display t        ; for subdirectory hiding
  1049.     mode-line-buffer-identification '("Dired: %17b"))
  1050.   (set (make-local-variable 'revert-buffer-function)
  1051.        (function dired-revert))
  1052.   (set (make-local-variable 'page-delimiter)
  1053.        "\n\n")
  1054.   (set (make-local-variable 'dired-directory)
  1055.        (or dirname default-directory))
  1056.   ;; list-buffers uses this to display the dir being edited in this buffer.
  1057.   (set (make-local-variable 'list-buffers-directory)
  1058.        (expand-file-name dired-directory))
  1059.   (set (make-local-variable 'dired-actual-switches)
  1060.        (or switches dired-listing-switches))
  1061.   (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
  1062.   (dired-sort-other dired-actual-switches t)
  1063.   (run-hooks 'dired-mode-hook))
  1064.  
  1065. ;; Idiosyncratic dired commands that don't deal with marks.
  1066.  
  1067. (defun dired-quit ()
  1068.   "Bury the current dired buffer."
  1069.   (interactive)
  1070.   (bury-buffer))
  1071.  
  1072. (defun dired-summary ()
  1073.   "Summarize basic Dired commands and show recent Dired errors."
  1074.   (interactive)
  1075.   (dired-why)
  1076.   ;>> this should check the key-bindings and use substitute-command-keys if non-standard
  1077.   (message
  1078.    "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
  1079.  
  1080. (defun dired-undo ()
  1081.   "Undo in a dired buffer.
  1082. This doesn't recover lost files, it just undoes changes in the buffer itself.
  1083. You can use it to recover marks, killed lines or subdirs.
  1084. In the latter case, you have to do \\[dired-build-subdir-alist] to
  1085. parse the buffer again."
  1086.   (interactive)
  1087.   (let (buffer-read-only)
  1088.     (undo)))
  1089.  
  1090. (defun dired-next-line (arg)
  1091.   "Move down lines then position at filename.
  1092. Optional prefix ARG says how many lines to move; default is one line."
  1093.   (interactive "p")
  1094.   (next-line arg)
  1095.   (dired-move-to-filename))
  1096.  
  1097. (defun dired-previous-line (arg)
  1098.   "Move up lines then position at filename.
  1099. Optional prefix ARG says how many lines to move; default is one line."
  1100.   (interactive "p")
  1101.   (previous-line arg)
  1102.   (dired-move-to-filename))
  1103.  
  1104. (defun dired-next-dirline (arg &optional opoint)
  1105.   "Goto ARG'th next directory file line."
  1106.   (interactive "p")
  1107.   (or opoint (setq opoint (point)))
  1108.   (if (if (> arg 0)
  1109.       (re-search-forward dired-re-dir nil t arg)
  1110.     (beginning-of-line)
  1111.     (re-search-backward dired-re-dir nil t (- arg)))
  1112.       (dired-move-to-filename)        ; user may type `i' or `f'
  1113.     (goto-char opoint)
  1114.     (error "No more subdirectories")))
  1115.  
  1116. (defun dired-prev-dirline (arg)
  1117.   "Goto ARG'th previous directory file line."
  1118.   (interactive "p")
  1119.   (dired-next-dirline (- arg)))
  1120.  
  1121. (defun dired-up-directory (&optional other-window)
  1122.   "Run dired on parent directory of current directory.
  1123. Find the parent directory either in this buffer or another buffer.
  1124. Creates a buffer if necessary."
  1125.   (interactive "P")
  1126.   (let* ((dir (dired-current-directory))
  1127.      (up (file-name-directory (directory-file-name dir))))
  1128.     (or (dired-goto-file (directory-file-name dir))
  1129.     ;; Only try dired-goto-subdir if buffer has more than one dir.
  1130.     (and (cdr dired-subdir-alist)
  1131.          (dired-goto-subdir up))
  1132.     (progn
  1133.       (if other-window
  1134.           (dired-other-window up)
  1135.         (dired up))
  1136.       (dired-goto-file dir)))))
  1137.  
  1138. ;; Force `f' rather than `e' in the mode doc:
  1139. (defalias 'dired-advertised-find-file 'dired-find-file)
  1140. (defun dired-find-file ()
  1141.   "In dired, visit the file or directory named on this line."
  1142.   (interactive)
  1143.   (find-file (file-name-sans-versions (dired-get-filename) t)))
  1144.  
  1145. (defun dired-mouse-find-file-other-window (event)
  1146.   "In dired, visit the file or directory name you click on."
  1147.   (interactive "e")
  1148.   (let (file)
  1149.     (save-excursion
  1150.       (set-buffer (window-buffer (posn-window (event-end event))))
  1151.       (save-excursion
  1152.     (goto-char (posn-point (event-end event)))
  1153.     (setq file (dired-get-filename))))
  1154.     (select-window (posn-window (event-end event)))
  1155.     (find-file-other-window (file-name-sans-versions file t))))
  1156.  
  1157. (defun dired-view-file ()
  1158.   "In dired, examine a file in view mode, returning to dired when done.
  1159. When file is a directory, show it in this buffer if it is inserted;
  1160. otherwise, display it in another buffer."
  1161.   (interactive)
  1162.   (if (file-directory-p (dired-get-filename))
  1163.       (or (and (cdr dired-subdir-alist)
  1164.            (dired-goto-subdir (dired-get-filename)))
  1165.       (dired (dired-get-filename)))
  1166.     (view-file (dired-get-filename))))
  1167.  
  1168. (defun dired-find-file-other-window ()
  1169.   "In dired, visit this file or directory in another window."
  1170.   (interactive)
  1171.   (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
  1172.  
  1173. (defun dired-display-file ()
  1174.   "In dired, display this file or directory in another window."
  1175.   (interactive)
  1176.   (let ((file (file-name-sans-versions (dired-get-filename) t)))
  1177.     (display-buffer (find-file-noselect file))))
  1178.  
  1179. ;;; Functions for extracting and manipulating file names in dired buffers.
  1180.  
  1181. (defun dired-get-filename (&optional localp no-error-if-not-filep)
  1182.   "In dired, return name of file mentioned on this line.
  1183. Value returned normally includes the directory name.
  1184. Optional arg LOCALP with value `no-dir' means don't include directory
  1185.   name in result.  A value of t means construct name relative to
  1186.   `default-directory', which still may contain slashes if in a subdirectory.
  1187. Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
  1188.   this line, otherwise an error occurs."
  1189.   (let (case-fold-search file p1 p2)
  1190.     (save-excursion
  1191.       (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
  1192.       (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
  1193.     ;; nil if no file on this line, but no-error-if-not-filep is t:
  1194.     (if (setq file (and p1 p2 (buffer-substring p1 p2)))
  1195.     (progn
  1196.       ;; Get rid of the mouse-face property that file names have.
  1197.       (set-text-properties 0 (length file) nil file)
  1198.       ;; Unquote names quoted by ls or by dired-insert-directory.
  1199.       ;; Using read to unquote is much faster than substituting
  1200.       ;; \007 (4 chars) -> ^G  (1 char) etc. in a lisp loop.
  1201.       (setq file
  1202.         (read
  1203.          (concat "\""
  1204.              ;; some ls -b don't escape quotes, argh!
  1205.              ;; This is not needed for GNU ls, though.
  1206.              (or (dired-string-replace-match
  1207.                   "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
  1208.                  file)
  1209.              "\"")))))
  1210.     (if (eq localp 'no-dir)
  1211.     file
  1212.       (and file (concat (dired-current-directory localp) file)))))
  1213.  
  1214. (defun dired-string-replace-match (regexp string newtext
  1215.                       &optional literal global)
  1216.   "Replace first match of REGEXP in STRING with NEWTEXT.
  1217. If it does not match, nil is returned instead of the new string.
  1218. Optional arg LITERAL means to take NEWTEXT literally.
  1219. Optional arg GLOBAL means to replace all matches."
  1220.   (if global
  1221.       (let ((start 0))
  1222.     (while (string-match regexp string start)
  1223.       (let ((from-end (- (length string) (match-end 0))))
  1224.         (setq string (replace-match newtext t literal string))
  1225.         (setq start (- (length string) from-end))))
  1226.       string)
  1227.     (if (not (string-match regexp string 0))
  1228.     nil
  1229.       (replace-match newtext t literal string))))
  1230.  
  1231. (defun dired-make-absolute (file &optional dir)
  1232.   ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
  1233.   ;; We can't always use expand-file-name as this would get rid of `.'
  1234.   ;; or expand in / instead default-directory if DIR=="".
  1235.   ;; This should be good enough for ange-ftp, but might easily be
  1236.   ;; redefined (for VMS?).
  1237.   ;; It should be reasonably fast, though, as it is called in
  1238.   ;; dired-get-filename.
  1239.   (concat (or dir default-directory) file))
  1240.  
  1241. (defun dired-make-relative (file &optional dir no-error)
  1242.   ;;"Convert FILE (an absolute pathname) to a pathname relative to DIR.
  1243.   ;; Else error (unless NO-ERROR is non-nil, then FILE is returned unchanged)
  1244.   ;;DIR defaults to default-directory."
  1245.   ;; DIR must be file-name-as-directory, as with all directory args in
  1246.   ;; Emacs Lisp code.
  1247.   (or dir (setq dir default-directory))
  1248.   ;; This case comes into play if default-directory is set to
  1249.   ;; use ~.
  1250.   (if (and (> (length dir) 0) (= (aref dir 0) ?~))
  1251.       (setq dir (expand-file-name dir)))
  1252.   (if (string-match (concat "^" (regexp-quote dir)) file)
  1253.       (substring file (match-end 0))
  1254.     (if no-error
  1255.     file
  1256.       (error "%s: not in directory tree growing at %s" file dir))))
  1257.  
  1258. ;;; Functions for finding the file name in a dired buffer line.
  1259.  
  1260. (defvar dired-move-to-filename-regexp
  1261.   "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+ [ 0-9][0-9][:0-9][0-9][ 0-9] "
  1262.   "Regular expression to match a month abbreviation followed by a number.")
  1263.  
  1264. (defconst dired-standard-move-to-filename-regexp
  1265.   "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+ [ 0-9][0-9][:0-9][0-9][ 0-9] "
  1266.   "Regular expression to match a month abbreviation followed by a number.")
  1267.  
  1268. ;; Move to first char of filename on this line.
  1269. ;; Returns position (point) or nil if no filename on this line."
  1270. (defun dired-move-to-filename (&optional raise-error eol)
  1271.   ;; This is the UNIX version.
  1272.   (or eol (setq eol (progn (end-of-line) (point))))
  1273.   (beginning-of-line)
  1274.   (if (re-search-forward dired-move-to-filename-regexp eol t)
  1275.       (goto-char (match-end 0))
  1276.     (if raise-error
  1277.     (error "No file on this line"))))
  1278.  
  1279. (defun dired-move-to-end-of-filename (&optional no-error)
  1280.   ;; Assumes point is at beginning of filename,
  1281.   ;; thus the rwx bit re-search-backward below will succeed in *this*
  1282.   ;; line if at all.  So, it should be called only after
  1283.   ;; (dired-move-to-filename t).
  1284.   ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
  1285.   ;; This is the UNIX version.
  1286.   (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
  1287.     ;; case-fold-search is nil now, so we can test for capital F:
  1288.     (setq used-F (string-match "F" dired-actual-switches)
  1289.       opoint (point)
  1290.           eol (save-excursion (end-of-line) (point))
  1291.       hidden (and selective-display
  1292.               (save-excursion (search-forward "\r" eol t))))
  1293.     (if hidden
  1294.     nil
  1295.       (save-excursion;; Find out what kind of file this is:
  1296.     ;; Restrict perm bits to be non-blank,
  1297.     ;; otherwise this matches one char to early (looking backward):
  1298.     ;; "l---------" (some systems make symlinks that way)
  1299.     ;; "----------" (plain file with zero perms)
  1300.     (if (re-search-backward
  1301.          "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
  1302.          nil t)
  1303.         (setq file-type (char-after (match-beginning 1))
  1304.           symlink (eq file-type ?l)
  1305.           ;; Only with -F we need to know whether it's an executable
  1306.           executable (and
  1307.                   used-F
  1308.                   (string-match
  1309.                    "[xst]";; execute bit set anywhere?
  1310.                    (concat
  1311.                 (buffer-substring (match-beginning 2)
  1312.                           (match-end 2))
  1313.                 (buffer-substring (match-beginning 3)
  1314.                           (match-end 3))
  1315.                 (buffer-substring (match-beginning 4)
  1316.                           (match-end 4))))))
  1317.       (or no-error (error "No file on this line"))))
  1318.       ;; Move point to end of name:
  1319.       (if symlink
  1320.       (if (search-forward " ->" eol t)
  1321.           (progn
  1322.         (forward-char -3)
  1323.         (and used-F
  1324.              dired-ls-F-marks-symlinks
  1325.              (eq (preceding-char) ?@);; did ls really mark the link?
  1326.              (forward-char -1))))
  1327.     (goto-char eol);; else not a symbolic link
  1328.     ;; ls -lF marks dirs, sockets and executables with exactly one
  1329.     ;; trailing character. (Executable bits on symlinks ain't mean
  1330.     ;; a thing, even to ls, but we know it's not a symlink.)
  1331.     (and used-F
  1332.          (or (memq file-type '(?d ?s))
  1333.          executable)
  1334.          (forward-char -1))))
  1335.     (or no-error
  1336.     (not (eq opoint (point)))
  1337.     (error (if hidden
  1338.            (substitute-command-keys
  1339.             "File line is hidden, type \\[dired-hide-subdir] to unhide")
  1340.          "No file on this line")))
  1341.     (if (eq opoint (point))
  1342.     nil
  1343.       (point))))
  1344.  
  1345.  
  1346. ;; Keeping Dired buffers in sync with the filesystem and with each other
  1347.  
  1348. (defvar dired-buffers nil
  1349.   ;; Enlarged by dired-advertise
  1350.   ;; Queried by function dired-buffers-for-dir. When this detects a
  1351.   ;; killed buffer, it is removed from this list.
  1352.   "Alist of expanded directories and their associated dired buffers.")
  1353.  
  1354. (defun dired-buffers-for-dir (dir)
  1355. ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
  1356. ;; The list is in reverse order of buffer creation, most recent last.
  1357. ;; As a side effect, killed dired buffers for DIR are removed from
  1358. ;; dired-buffers.
  1359.   (setq dir (file-name-as-directory dir))
  1360.   (let ((alist dired-buffers) result elt buf)
  1361.     (while alist
  1362.       (setq elt (car alist)
  1363.         buf (cdr elt))
  1364.       (if (buffer-name buf)
  1365.       (if (dired-in-this-tree dir (car elt))
  1366.           (if (assoc dir (save-excursion
  1367.                    (set-buffer buf)
  1368.                    dired-subdir-alist))
  1369.           (setq result (cons buf result))))
  1370.     ;; else buffer is killed - clean up:
  1371.     (setq dired-buffers (delq elt dired-buffers)))
  1372.       (setq alist (cdr alist)))
  1373.     result))
  1374.  
  1375. (defun dired-advertise ()
  1376.   ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
  1377.   ;; With wildcards we actually advertise too much.
  1378.   (let ((expanded-default (expand-file-name default-directory)))
  1379.     (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
  1380.     t                ; we have already advertised ourselves
  1381.       (setq dired-buffers
  1382.         (cons (cons expanded-default (current-buffer))
  1383.           dired-buffers)))))
  1384.  
  1385. (defun dired-unadvertise (dir)
  1386.   ;; Remove DIR from the buffer alist in variable dired-buffers.
  1387.   ;; This has the effect of removing any buffer whose main directory is DIR.
  1388.   ;; It does not affect buffers in which DIR is a subdir.
  1389.   ;; Removing is also done as a side-effect in dired-buffer-for-dir.
  1390.   (setq dired-buffers
  1391.     (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
  1392.  
  1393. ;; Tree Dired
  1394.  
  1395. ;;; utility functions
  1396.  
  1397. (defun dired-in-this-tree (file dir)
  1398.   ;;"Is FILE part of the directory tree starting at DIR?"
  1399.   (let (case-fold-search)
  1400.     (string-match (concat "^" (regexp-quote dir)) file)))
  1401.  
  1402. (defun dired-normalize-subdir (dir)
  1403.   ;; Prepend default-directory to DIR if relative path name.
  1404.   ;; dired-get-filename must be able to make a valid filename from a
  1405.   ;; file and its directory DIR.
  1406.   (file-name-as-directory
  1407.    (if (file-name-absolute-p dir)
  1408.        dir
  1409.      (expand-file-name dir default-directory))))
  1410.  
  1411. (defun dired-get-subdir ()
  1412.   ;;"Return the subdir name on this line, or nil if not on a headerline."
  1413.   ;; Look up in the alist whether this is a headerline.
  1414.   (save-excursion
  1415.     (let ((cur-dir (dired-current-directory)))
  1416.       (beginning-of-line)        ; alist stores b-o-l positions
  1417.       (and (zerop (- (point)
  1418.              (dired-get-subdir-min (assoc cur-dir
  1419.                           dired-subdir-alist))))
  1420.        cur-dir))))
  1421.  
  1422. ;(defun dired-get-subdir-min (elt)
  1423. ;  (cdr elt))
  1424. ;; can't use macro,  must be redefinable for other alist format in dired-nstd.
  1425. (defalias 'dired-get-subdir-min 'cdr)
  1426.  
  1427. (defun dired-get-subdir-max (elt)
  1428.   (save-excursion
  1429.     (goto-char (dired-get-subdir-min elt))
  1430.     (dired-subdir-max)))
  1431.  
  1432. (defun dired-clear-alist ()
  1433.   (while dired-subdir-alist
  1434.     (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
  1435.     (setq dired-subdir-alist (cdr dired-subdir-alist))))
  1436.  
  1437. (defun dired-subdir-index (dir)
  1438.   ;; Return an index into alist for use with nth
  1439.   ;; for the sake of subdir moving commands.
  1440.   (let (found (index 0) (alist dired-subdir-alist))
  1441.     (while alist
  1442.       (if (string= dir (car (car alist)))
  1443.       (setq alist nil found t)
  1444.     (setq alist (cdr alist) index (1+ index))))
  1445.     (if found index nil)))
  1446.  
  1447. (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
  1448.   "Go to next subdirectory, regardless of level."
  1449.   ;; Use 0 arg to go to this directory's header line.
  1450.   ;; NO-SKIP prevents moving to end of header line, returning whatever
  1451.   ;; position was found in dired-subdir-alist.
  1452.   (interactive "p")
  1453.   (let ((this-dir (dired-current-directory))
  1454.     pos index)
  1455.     ;; nth with negative arg does not return nil but the first element
  1456.     (setq index (- (dired-subdir-index this-dir) arg))
  1457.     (setq pos (if (>= index 0)
  1458.           (dired-get-subdir-min (nth index dired-subdir-alist))))
  1459.     (if pos
  1460.     (progn
  1461.       (goto-char pos)
  1462.       (or no-skip (skip-chars-forward "^\n\r"))
  1463.       (point))
  1464.       (if no-error-if-not-found
  1465.       nil                ; return nil if not found
  1466.     (error "%s directory" (if (> arg 0) "Last" "First"))))))
  1467.  
  1468. (defun dired-build-subdir-alist ()
  1469.   "Build `dired-subdir-alist' by parsing the buffer.
  1470. Returns the new value of the alist."
  1471.   (interactive)
  1472.   (dired-clear-alist)
  1473.   (save-excursion
  1474.     (let ((count 0)
  1475.       (buffer-read-only nil)
  1476.       new-dir-name)
  1477.       (goto-char (point-min))
  1478.       (setq dired-subdir-alist nil)
  1479.       (while (and (re-search-forward dired-subdir-regexp nil t)
  1480.           ;; Avoid taking a file name ending in a colon
  1481.           ;; as a subdir name.
  1482.           (not (save-excursion
  1483.              (goto-char (match-beginning 0))
  1484.              (beginning-of-line)
  1485.              (forward-char 2)
  1486.              (save-match-data (looking-at dired-re-perms)))))
  1487.     (save-excursion
  1488.       (goto-char (match-beginning 1))
  1489.       (setq new-dir-name
  1490.         (expand-file-name (buffer-substring (point) (match-end 1))))
  1491.       (delete-region (point) (match-end 1))
  1492.       (insert new-dir-name))
  1493.     (setq count (1+ count))
  1494.     (dired-alist-add-1 new-dir-name
  1495.                ;; Place a sub directory boundary between lines.
  1496.                (save-excursion
  1497.                  (goto-char (match-beginning 0))
  1498.                  (beginning-of-line)
  1499.                  (point-marker))))
  1500.       (if (> count 1)
  1501.       (message "Buffer includes %d directories" count))
  1502.       ;; We don't need to sort it because it is in buffer order per
  1503.       ;; constructionem.  Return new alist:
  1504.       dired-subdir-alist)))
  1505.  
  1506. (defun dired-alist-add-1 (dir new-marker)
  1507.   ;; Add new DIR at NEW-MARKER.  Don't sort.
  1508.   (setq dired-subdir-alist
  1509.     (cons (cons (dired-normalize-subdir dir) new-marker)
  1510.           dired-subdir-alist)))
  1511.  
  1512. (defun dired-goto-next-nontrivial-file ()
  1513.   ;; Position point on first nontrivial file after point.
  1514.   (dired-goto-next-file);; so there is a file to compare with
  1515.   (if (stringp dired-trivial-filenames)
  1516.       (while (and (not (eobp))
  1517.           (string-match dired-trivial-filenames
  1518.                 (file-name-nondirectory
  1519.                  (or (dired-get-filename nil t) ""))))
  1520.     (forward-line 1)
  1521.     (dired-move-to-filename))))
  1522.  
  1523. (defun dired-goto-next-file ()
  1524.   (let ((max (1- (dired-subdir-max))))
  1525.     (while (and (not (dired-move-to-filename)) (< (point) max))
  1526.       (forward-line 1))))
  1527.  
  1528. (defun dired-goto-file (file)
  1529.   "Go to file line of FILE in this dired buffer."
  1530.   ;; Return value of point on success, else nil.
  1531.   ;; FILE must be an absolute pathname.
  1532.   ;; Loses if FILE contains control chars like "\007" for which ls
  1533.   ;; either inserts "?" or "\\007" into the buffer, so we won't find
  1534.   ;; it in the buffer.
  1535.   (interactive
  1536.    (prog1                ; let push-mark display its message
  1537.        (list (expand-file-name
  1538.           (read-file-name "Goto file: "
  1539.                   (dired-current-directory))))
  1540.      (push-mark)))
  1541.   (setq file (directory-file-name file)) ; does no harm if no directory
  1542.   (let (found case-fold-search dir)
  1543.     (setq dir (or (file-name-directory file)
  1544.           (error "Need absolute pathname for %s" file)))
  1545.     (save-excursion
  1546.       ;; The hair here is to get the result of dired-goto-subdir
  1547.       ;; without really calling it if we don't have any subdirs.
  1548.       (if (if (string= dir (expand-file-name default-directory))
  1549.           (goto-char (point-min))
  1550.         (and (cdr dired-subdir-alist)
  1551.          (dired-goto-subdir dir)))
  1552.       (let ((base (file-name-nondirectory file))
  1553.         (boundary (dired-subdir-max)))
  1554.         (while (and (not found)
  1555.             ;; filenames are preceded by SPC, this makes
  1556.             ;; the search faster (e.g. for the filename "-"!).
  1557.              (search-forward (concat " " base) boundary 'move))
  1558.           ;; Match could have BASE just as initial substring or
  1559.           ;; or in permission bits or date or
  1560.           ;; not be a proper filename at all:
  1561.           (if (equal base (dired-get-filename 'no-dir t))
  1562.             ;; Must move to filename since an (actually
  1563.             ;; correct) match could have been elsewhere on the
  1564.             ;; ;; line (e.g. "-" would match somewhere in the
  1565.             ;; permission bits).
  1566.           (setq found (dired-move-to-filename))
  1567.         ;; If this isn't the right line, move forward to avoid
  1568.         ;; trying this line again.
  1569.         (forward-line 1))))))
  1570.     (and found
  1571.      ;; return value of point (i.e., FOUND):
  1572.      (goto-char found))))
  1573.  
  1574. (defun dired-initial-position (dirname)
  1575.   ;; Where point should go in a new listing of DIRNAME.
  1576.   ;; Point assumed at beginning of new subdir line.
  1577.   ;; You may redefine this function as you wish, e.g. like in dired-x.el.
  1578.   (end-of-line)
  1579.   (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
  1580.  
  1581. ;; These are hooks which make tree dired work.
  1582. ;; They are in this file because other parts of dired need to call them.
  1583. ;; But they don't call the rest of tree dired unless there are subdirs loaded.
  1584.  
  1585. ;; This function is called for each retrieved filename.
  1586. ;; It could stand to be faster, though it's mostly function call
  1587. ;; overhead.  Avoiding the function call seems to save about 10% in
  1588. ;; dired-get-filename.  Make it a defsubst?
  1589. (defun dired-current-directory (&optional localp)
  1590.   "Return the name of the subdirectory to which this line belongs.
  1591. This returns a string with trailing slash, like `default-directory'.
  1592. Optional argument means return a file name relative to `default-directory'."
  1593.   (let ((here (point))
  1594.     (alist (or dired-subdir-alist
  1595.            ;; probably because called in a non-dired buffer
  1596.            (error "No subdir-alist in %s" (current-buffer))))
  1597.     elt dir)
  1598.     (while alist
  1599.       (setq elt (car alist)
  1600.         dir (car elt)
  1601.         ;; use `<=' (not `<') as subdir line is part of subdir
  1602.         alist (if (<= (dired-get-subdir-min elt) here)
  1603.               nil        ; found
  1604.             (cdr alist))))
  1605.     (if localp
  1606.     (dired-make-relative dir default-directory)
  1607.       dir)))
  1608.  
  1609. ;; Subdirs start at the beginning of their header lines and end just
  1610. ;; before the beginning of the next header line (or end of buffer).
  1611.  
  1612. (defun dired-subdir-max ()
  1613.   (save-excursion
  1614.     (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
  1615.     (point-max)
  1616.       (point))))
  1617.  
  1618. ;; Deleting files
  1619.  
  1620. (defun dired-do-flagged-delete (&optional nomessage)
  1621.   "In dired, delete the files flagged for deletion.
  1622. If NOMESSAGE is non-nil, we don't display any message
  1623. if there are no flagged files."
  1624.   (interactive)
  1625.   (let* ((dired-marker-char dired-del-marker)
  1626.      (regexp (dired-marker-regexp))
  1627.      case-fold-search)
  1628.     (if (save-excursion (goto-char (point-min))
  1629.             (re-search-forward regexp nil t))
  1630.     (dired-internal-do-deletions
  1631.      ;; this can't move point since ARG is nil
  1632.      (dired-map-over-marks (cons (dired-get-filename) (point))
  1633.                    nil)
  1634.      nil)
  1635.       (or nomessage
  1636.       (message "(No deletions requested)")))))
  1637.  
  1638. (defun dired-do-delete (&optional arg)
  1639.   "Delete all marked (or next ARG) files."
  1640.   ;; This is more consistent with the file marking feature than
  1641.   ;; dired-do-flagged-delete.
  1642.   (interactive "P")
  1643.   (dired-internal-do-deletions
  1644.    ;; this may move point if ARG is an integer
  1645.    (dired-map-over-marks (cons (dired-get-filename) (point))
  1646.              arg)
  1647.    arg))
  1648.  
  1649. (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
  1650.  
  1651. (defun dired-internal-do-deletions (l arg)
  1652.   ;; L is an alist of files to delete, with their buffer positions.
  1653.   ;; ARG is the prefix arg.
  1654.   ;; Filenames are absolute (VMS needs this for logical search paths).
  1655.   ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
  1656.   ;; That way as changes are made in the buffer they do not shift the
  1657.   ;; lines still to be changed, so the (point) values in L stay valid.
  1658.   ;; Also, for subdirs in natural order, a subdir's files are deleted
  1659.   ;; before the subdir itself - the other way around would not work.
  1660.   (let ((files (mapcar (function car) l))
  1661.     (count (length l))
  1662.     (succ 0))
  1663.     ;; canonicalize file list for pop up
  1664.     (setq files (nreverse (mapcar (function dired-make-relative) files)))
  1665.     (if (dired-mark-pop-up
  1666.      " *Deletions*" 'delete files dired-deletion-confirmer
  1667.      (format "Delete %s " (dired-mark-prompt arg files)))
  1668.     (save-excursion
  1669.       (let (failures);; files better be in reverse order for this loop!
  1670.         (while l
  1671.           (goto-char (cdr (car l)))
  1672.           (let (buffer-read-only)
  1673.         (condition-case err
  1674.             (let ((fn (car (car l))))
  1675.               ;; This test is equivalent to
  1676.               ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  1677.               ;; but more efficient
  1678.               (if (eq t (car (file-attributes fn)))
  1679.               (delete-directory fn)
  1680.             (delete-file fn))
  1681.               ;; if we get here, removing worked
  1682.               (setq succ (1+ succ))
  1683.               (message "%s of %s deletions" succ count)
  1684.               (delete-region (progn (beginning-of-line) (point))
  1685.                      (progn (forward-line 1) (point)))
  1686.               (dired-clean-up-after-deletion fn))
  1687.           (error;; catch errors from failed deletions
  1688.            (dired-log "%s\n" err)
  1689.            (setq failures (cons (car (car l)) failures)))))
  1690.           (setq l (cdr l)))
  1691.         (if (not failures)
  1692.         (message "%d deletion%s done" count (dired-plural-s count))
  1693.           (dired-log-summary
  1694.            (format "%d of %d deletion%s failed"
  1695.                (length failures) count
  1696.                (dired-plural-s count))
  1697.            failures))))
  1698.       (message "(No deletions performed)")))
  1699.   (dired-move-to-filename))
  1700.  
  1701. ;; This is a separate function for the sake of dired-x.el.
  1702. (defun dired-clean-up-after-deletion (fn)
  1703.   ;; Clean up after a deleted file or directory FN.
  1704.   (save-excursion (and (cdr dired-subdir-alist)
  1705.                (dired-goto-subdir fn)
  1706.                (dired-kill-subdir))))
  1707.  
  1708. ;; Confirmation
  1709.  
  1710. (defun dired-marker-regexp ()
  1711.   (concat "^" (regexp-quote (char-to-string dired-marker-char))))
  1712.  
  1713. (defun dired-plural-s (count)
  1714.   (if (= 1 count) "" "s"))
  1715.  
  1716. (defun dired-mark-prompt (arg files)
  1717.   ;; Return a string for use in a prompt, either the current file
  1718.   ;; name, or the marker and a count of marked files.
  1719.   (let ((count (length files)))
  1720.     (if (= count 1)
  1721.     (car files)
  1722.       ;; more than 1 file:
  1723.       (if (integerp arg)
  1724.       ;; abs(arg) = count
  1725.       ;; Perhaps this is nicer, but it also takes more screen space:
  1726.       ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
  1727.       ;;                        count)
  1728.       (format "[next %d files]" arg)
  1729.     (format "%c [%d files]" dired-marker-char count)))))
  1730.  
  1731. (defun dired-pop-to-buffer (buf)
  1732.   ;; Pop up buffer BUF.
  1733.   ;; If dired-shrink-to-fit is t, make its window fit its contents.
  1734.   (if (not dired-shrink-to-fit)
  1735.       (pop-to-buffer (get-buffer-create buf))
  1736.     ;; let window shrink to fit:
  1737.     (let ((window (selected-window))
  1738.       target-lines w2)
  1739.       (cond ;; if split-window-threshold is enabled, use the largest window
  1740.             ((and (> (window-height (setq w2 (get-largest-window)))
  1741.              split-height-threshold)
  1742.           (= (frame-width) (window-width w2)))
  1743.          (setq window w2))
  1744.         ;; if the least-recently-used window is big enough, use it
  1745.         ((and (> (window-height (setq w2 (get-lru-window)))
  1746.              (* 2 window-min-height))
  1747.           (= (frame-width) (window-width w2)))
  1748.          (setq window w2)))
  1749.       (save-excursion
  1750.     (set-buffer buf)
  1751.     (goto-char (point-max))
  1752.     (skip-chars-backward "\n\r\t ")
  1753.     (setq target-lines (count-lines (point-min) (point)))
  1754.     ;; Don't forget to count the last line.
  1755.     (if (not (bolp))
  1756.         (setq target-lines (1+ target-lines))))
  1757.       (if (<= (window-height window) (* 2 window-min-height))
  1758.       ;; At this point, every window on the frame is too small to split.
  1759.       (setq w2 (display-buffer buf))
  1760.     (setq w2 (split-window window
  1761.           (max window-min-height
  1762.                (- (window-height window)
  1763.               (1+ (max window-min-height target-lines)))))))
  1764.       (set-window-buffer w2 buf)
  1765.       (if (< (1- (window-height w2)) target-lines)
  1766.       (progn
  1767.         (select-window w2)
  1768.         (enlarge-window (- target-lines (1- (window-height w2))))))
  1769.       (set-window-start w2 1)
  1770.       )))
  1771.  
  1772. (defvar dired-no-confirm nil
  1773. ;;  "If non-nil, list of symbols for commands dired should not confirm.
  1774. ;;It can be a sublist of
  1775. ;;
  1776. ;;  '(byte-compile chgrp chmod chown compress copy delete hardlink load
  1777. ;;    move print shell symlink uncompress)"
  1778.   )
  1779.  
  1780. (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
  1781.   ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
  1782.   ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
  1783.   ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
  1784.   ;;files.  Uses function `dired-pop-to-buffer' to do that.
  1785.   ;; FUNCTION should not manipulate files.
  1786.   ;; It should only read input (an argument or confirmation).
  1787.   ;;The window is not shown if there is just one file or
  1788.   ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
  1789.   ;;FILES is the list of marked files."
  1790.   (or bufname (setq bufname  " *Marked Files*"))
  1791.   (if (or (memq op-symbol dired-no-confirm)
  1792.       (= (length files) 1))
  1793.       (apply function args)
  1794.     (save-excursion
  1795.       (set-buffer (get-buffer-create bufname))
  1796.       (erase-buffer)
  1797.       (dired-format-columns-of-files files)
  1798.       (remove-text-properties (point-min) (point-max) '(mouse-face)))
  1799.     (save-window-excursion
  1800.       (dired-pop-to-buffer bufname)
  1801.       (apply function args))))
  1802.  
  1803. (defun dired-format-columns-of-files (files)
  1804.   ;; Files should be in forward order for this loop.
  1805.   ;; i.e., (car files) = first file in buffer.
  1806.   ;; Returns the number of lines used.
  1807.   (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
  1808.      (width (- (window-width (selected-window)) 2))
  1809.      (columns (max 1 (/ width maxlen)))
  1810.      (nfiles (length files))
  1811.      (rows (+ (/ nfiles columns)
  1812.           (if (zerop (% nfiles columns)) 0 1)))
  1813.      (i 0)
  1814.      (j 0))
  1815.     (setq files (nconc (copy-sequence files) ; fill up with empty fns
  1816.                (make-list (- (* columns rows) nfiles) "")))
  1817.     (setcdr (nthcdr (1- (length files)) files) files) ; make circular
  1818.     (while (< j rows)
  1819.       (while (< i columns)
  1820.     (indent-to (* i maxlen))
  1821.     (insert (car files))
  1822.     (setq files (nthcdr rows files)
  1823.           i (1+ i)))
  1824.       (insert "\n")
  1825.       (setq i 0
  1826.         j (1+ j)
  1827.         files (cdr files)))
  1828.     rows))
  1829.  
  1830. ;; Commands to mark or flag file(s) at or near current line.
  1831.  
  1832. (defun dired-repeat-over-lines (arg function)
  1833.   ;; This version skips non-file lines.
  1834.   (let ((pos (make-marker)))
  1835.     (beginning-of-line)
  1836.     (while (and (> arg 0) (not (eobp)))
  1837.       (setq arg (1- arg))
  1838.       (beginning-of-line)
  1839.       (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
  1840.       (save-excursion
  1841.     (forward-line 1)
  1842.     (move-marker pos (1+ (point))))
  1843.       (save-excursion (funcall function))
  1844.       ;; Advance to the next line--actually, to the line that *was* next.
  1845.       ;; (If FUNCTION inserted some new lines in between, skip them.)
  1846.       (goto-char pos))
  1847.     (while (and (< arg 0) (not (bobp)))
  1848.       (setq arg (1+ arg))
  1849.       (forward-line -1)
  1850.       (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
  1851.       (beginning-of-line)
  1852.       (save-excursion (funcall function)))
  1853.     (move-marker pos nil)
  1854.     (dired-move-to-filename)))
  1855.  
  1856. (defun dired-between-files ()
  1857.   ;; Point must be at beginning of line
  1858.   ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
  1859.   ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
  1860.   (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
  1861.       (and (looking-at dired-subdir-regexp)
  1862.        (save-excursion (not (dired-move-to-filename))))))
  1863.  
  1864. (defun dired-next-marked-file (arg &optional wrap opoint)
  1865.   "Move to the next marked file, wrapping around the end of the buffer."
  1866.   (interactive "p\np")
  1867.   (or opoint (setq opoint (point)));; return to where interactively started
  1868.   (if (if (> arg 0)
  1869.       (re-search-forward dired-re-mark nil t arg)
  1870.     (beginning-of-line)
  1871.     (re-search-backward dired-re-mark nil t (- arg)))
  1872.       (dired-move-to-filename)
  1873.     (if (null wrap)
  1874.     (progn
  1875.       (goto-char opoint)
  1876.       (error "No next marked file"))
  1877.       (message "(Wraparound for next marked file)")
  1878.       (goto-char (if (> arg 0) (point-min) (point-max)))
  1879.       (dired-next-marked-file arg nil opoint))))
  1880.  
  1881. (defun dired-prev-marked-file (arg &optional wrap)
  1882.   "Move to the previous marked file, wrapping around the end of the buffer."
  1883.   (interactive "p\np")
  1884.   (dired-next-marked-file (- arg) wrap))
  1885.  
  1886. (defun dired-file-marker (file)
  1887.   ;; Return FILE's marker, or nil if unmarked.
  1888.   (save-excursion
  1889.     (and (dired-goto-file file)
  1890.      (progn
  1891.        (beginning-of-line)
  1892.        (if (not (equal ?\040 (following-char)))
  1893.            (following-char))))))
  1894.  
  1895. (defun dired-mark-files-in-region (start end)
  1896.   (let (buffer-read-only)
  1897.     (if (> start end)
  1898.     (error "start > end"))
  1899.     (goto-char start)            ; assumed at beginning of line
  1900.     (while (< (point) end)
  1901.       ;; Skip subdir line and following garbage like the `total' line:
  1902.       (while (and (< (point) end) (dired-between-files))
  1903.     (forward-line 1))
  1904.       (if (and (not (looking-at dired-re-dot))
  1905.            (dired-get-filename nil t))
  1906.       (progn
  1907.         (delete-char 1)
  1908.         (insert dired-marker-char)))
  1909.       (forward-line 1))))
  1910.  
  1911. (defun dired-mark (arg)
  1912.   "Mark the current (or next ARG) files.
  1913. If on a subdir headerline, mark all its files except `.' and `..'.
  1914.  
  1915. Use \\[dired-unmark-all-files] to remove all marks
  1916. and \\[dired-unmark] on a subdir to remove the marks in
  1917. this subdir."
  1918.   (interactive "P")
  1919.   (if (dired-get-subdir)
  1920.       (save-excursion (dired-mark-subdir-files))
  1921.     (let (buffer-read-only)
  1922.       (dired-repeat-over-lines
  1923.        (prefix-numeric-value arg)
  1924.        (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
  1925.  
  1926. (defun dired-unmark (arg)
  1927.   "Unmark the current (or next ARG) files.
  1928. If looking at a subdir, unmark all its files except `.' and `..'."
  1929.   (interactive "P")
  1930.   (let ((dired-marker-char ?\040))
  1931.     (dired-mark arg)))
  1932.  
  1933. (defun dired-flag-file-deletion (arg)
  1934.   "In dired, flag the current line's file for deletion.
  1935. With prefix arg, repeat over several lines.
  1936.  
  1937. If on a subdir headerline, mark all its files except `.' and `..'."
  1938.   (interactive "P")
  1939.   (let ((dired-marker-char dired-del-marker))
  1940.     (dired-mark arg)))
  1941.  
  1942. (defun dired-unmark-backward (arg)
  1943.   "In dired, move up lines and remove deletion flag there.
  1944. Optional prefix ARG says how many lines to unflag; default is one line."
  1945.   (interactive "p")
  1946.   (dired-unmark (- arg)))
  1947.  
  1948. ;;; Commands to mark or flag files based on their characteristics or names.
  1949.  
  1950. (defvar dired-regexp-history nil
  1951.   "History list of regular expressions used in Dired commands.")
  1952.  
  1953. (defun dired-read-regexp (prompt)
  1954.   (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
  1955.  
  1956. (defun dired-mark-files-regexp (regexp &optional marker-char)
  1957.   "Mark all files matching REGEXP for use in later commands.
  1958. A prefix argument means to unmark them instead.
  1959. `.' and `..' are never marked.
  1960.  
  1961. REGEXP is an Emacs regexp, not a shell wildcard.  Thus, use `\\.o$' for
  1962. object files--just `.o' will mark more than you might think."
  1963.   (interactive
  1964.    (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
  1965.                     " files (regexp): "))
  1966.      (if current-prefix-arg ?\040)))
  1967.   (let ((dired-marker-char (or marker-char dired-marker-char)))
  1968.     (dired-mark-if
  1969.      (and (not (looking-at dired-re-dot))
  1970.       (not (eolp))            ; empty line
  1971.       (let ((fn (dired-get-filename nil t)))
  1972.         (and fn (string-match regexp (file-name-nondirectory fn)))))
  1973.      "matching file")))
  1974.  
  1975. (defun dired-flag-files-regexp (regexp)
  1976.   "In dired, flag all files containing the specified REGEXP for deletion.
  1977. The match is against the non-directory part of the filename.  Use `^'
  1978.   and `$' to anchor matches.  Exclude subdirs by hiding them.
  1979. `.' and `..' are never flagged."
  1980.   (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
  1981.   (dired-mark-files-regexp regexp dired-del-marker))
  1982.  
  1983. (defun dired-mark-symlinks (unflag-p)
  1984.   "Mark all symbolic links.
  1985. With prefix argument, unflag all those files."
  1986.   (interactive "P")
  1987.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  1988.     (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
  1989.  
  1990. (defun dired-mark-directories (unflag-p)
  1991.   "Mark all directory file lines except `.' and `..'.
  1992. With prefix argument, unflag all those files."
  1993.   (interactive "P")
  1994.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  1995.     (dired-mark-if (and (looking-at dired-re-dir)
  1996.             (not (looking-at dired-re-dot)))
  1997.            "directory file")))
  1998.  
  1999. (defun dired-mark-executables (unflag-p)
  2000.   "Mark all executable files.
  2001. With prefix argument, unflag all those files."
  2002.   (interactive "P")
  2003.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  2004.     (dired-mark-if (looking-at dired-re-exe) "executable file")))
  2005.  
  2006. ;; dired-x.el has a dired-mark-sexp interactive command: mark
  2007. ;; files for which PREDICATE returns non-nil.
  2008.  
  2009. (defun dired-flag-auto-save-files (&optional unflag-p)
  2010.   "Flag for deletion files whose names suggest they are auto save files.
  2011. A prefix argument says to unflag those files instead."
  2012.   (interactive "P")
  2013.   (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
  2014.     (dired-mark-if
  2015.      ;; It is less than general to check for # here,
  2016.      ;; but it's the only way this runs fast enough.
  2017.      (and (save-excursion (end-of-line)
  2018.                           (or
  2019.                            (eq (preceding-char) ?#)
  2020.                            ;; Handle executables in case of -F option.
  2021.                            ;; We need not worry about the other kinds
  2022.                            ;; of markings that -F makes, since they won't
  2023.                            ;; appear on real auto-save files.
  2024.                            (if (eq (preceding-char) ?*)
  2025.                                (progn
  2026.                                  (forward-char -1)
  2027.                                  (eq (preceding-char) ?#)))))
  2028.       (not (looking-at dired-re-dir))
  2029.       (let ((fn (dired-get-filename t t)))
  2030.         (if fn (auto-save-file-name-p
  2031.             (file-name-nondirectory fn)))))
  2032.      "auto save file")))
  2033.  
  2034. (defun dired-flag-backup-files (&optional unflag-p)
  2035.   "Flag all backup files (names ending with `~') for deletion.
  2036. With prefix argument, unflag these files."
  2037.   (interactive "P")
  2038.   (let ((dired-marker-char (if unflag-p ?\  dired-del-marker)))
  2039.     (dired-mark-if
  2040.      ;; Don't call backup-file-name-p unless the last character looks like
  2041.      ;; it might be the end of a backup file name.  This isn't very general,
  2042.      ;; but it's the only way this runs fast enough.
  2043.      (and (save-excursion (end-of-line)
  2044.               ;; Handle executables in case of -F option.
  2045.               ;; We need not worry about the other kinds
  2046.               ;; of markings that -F makes, since they won't
  2047.               ;; appear on real backup files.
  2048.               (if (eq (preceding-char) ?*)
  2049.                   (forward-char -1))
  2050.               (eq (preceding-char) ?~))
  2051.       (not (looking-at dired-re-dir))
  2052.       (let ((fn (dired-get-filename t t)))
  2053.         (if fn (backup-file-name-p fn))))
  2054.      "backup file")))
  2055.  
  2056. (defun dired-change-marks (&optional old new)
  2057.   "Change all OLD marks to NEW marks.
  2058. OLD and NEW are both characters used to mark files."
  2059.   (interactive
  2060.    (let* ((cursor-in-echo-area t)
  2061.       (old (progn (message "Change (old mark): ") (read-char)))
  2062.       (new (progn (message  "Change %c marks to (new mark): " old)
  2063.               (read-char))))
  2064.      (list old new)))
  2065.   (if (or (eq old ?\r) (eq new ?\r))
  2066.       (ding)
  2067.     (let ((string (format "\n%c" old))
  2068.       (buffer-read-only))
  2069.       (save-excursion
  2070.     (goto-char (point-min))
  2071.     (while (search-forward string nil t)
  2072.       (if (if (= old ?\ )
  2073.           (save-match-data
  2074.             (dired-get-filename 'no-dir t))
  2075.         t)
  2076.           (subst-char-in-region (match-beginning 0)
  2077.                     (match-end 0) old new)))))))
  2078.  
  2079. (defun dired-unmark-all-files-no-query ()
  2080.   "Remove all marks from all files in the Dired buffer."
  2081.   (interactive)
  2082.   (dired-unmark-all-files ?\r))
  2083.  
  2084. (defun dired-unmark-all-files (mark &optional arg)
  2085.   "Remove a specific mark (or any mark) from every file.
  2086. After this command, type the mark character to remove, 
  2087. or type RET to remove all marks.
  2088. With prefix arg, query for each marked file.
  2089. Type \\[help-command] at that time for help."
  2090.   (interactive "cRemove marks (RET means all): \nP")
  2091.   (save-excursion
  2092.     (let* ((count 0)
  2093.        buffer-read-only case-fold-search query
  2094.        (string (format "\n%c" mark))
  2095.        (help-form "\
  2096. Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
  2097. `!' to unmark all remaining files with no more questions."))
  2098.       (goto-char (point-min))
  2099.       (while (if (eq mark ?\r)
  2100.          (re-search-forward dired-re-mark nil t)
  2101.            (search-forward string nil t))
  2102.     (if (or (not arg)
  2103.         (dired-query 'query "Unmark file `%s'? "
  2104.                  (dired-get-filename t)))
  2105.         (progn (subst-char-in-region (1- (point)) (point)
  2106.                      (preceding-char) ?\ )
  2107.            (setq count (1+ count)))))
  2108.       (message (if (= count 1) "1 mark removed"
  2109.          "%d marks removed")
  2110.            count))))
  2111.  
  2112. ;; Logging failures operating on files, and showing the results.
  2113.  
  2114. (defvar dired-log-buffer "*Dired log*")
  2115.  
  2116. (defun dired-why ()
  2117.   "Pop up a buffer with error log output from Dired.
  2118. A group of errors from a single command ends with a formfeed.
  2119. Thus, use \\[backward-page] to find the beginning of a group of errors."
  2120.   (interactive)
  2121.   (if (get-buffer dired-log-buffer)
  2122.       (let ((owindow (selected-window))
  2123.         (window (display-buffer (get-buffer dired-log-buffer))))
  2124.     (unwind-protect
  2125.         (progn
  2126.           (select-window window)
  2127.           (goto-char (point-max))
  2128.           (recenter -1))
  2129.       (select-window owindow)))))
  2130.  
  2131. (defun dired-log (log &rest args)
  2132.   ;; Log a message or the contents of a buffer.
  2133.   ;; If LOG is a string and there are more args, it is formatted with
  2134.   ;; those ARGS.  Usually the LOG string ends with a \n.
  2135.   ;; End each bunch of errors with (dired-log t): this inserts
  2136.   ;; current time and buffer, and a \f (formfeed).
  2137.   (let ((obuf (current-buffer)))
  2138.     (unwind-protect            ; want to move point
  2139.     (progn
  2140.       (set-buffer (get-buffer-create dired-log-buffer))
  2141.       (goto-char (point-max))
  2142.       (let (buffer-read-only)
  2143.         (cond ((stringp log)
  2144.            (insert (if args
  2145.                    (apply (function format) log args)
  2146.                  log)))
  2147.           ((bufferp log)
  2148.            (insert-buffer log))
  2149.           ((eq t log)
  2150.            (insert "\n\t" (current-time-string)
  2151.                "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
  2152.       (set-buffer obuf))))
  2153.  
  2154. (defun dired-log-summary (string failures)
  2155.   (message (if failures "%s--type ? for details (%s)"
  2156.          "%s--type ? for details")
  2157.        string failures)
  2158.   ;; Log a summary describing a bunch of errors.
  2159.   (dired-log (concat "\n" string))
  2160.   (dired-log t))
  2161.  
  2162. ;;; Sorting
  2163.  
  2164. ;; Most ls can only sort by name or by date (with -t), nothing else.
  2165. ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
  2166. ;; So anything that does not contain these is sort "by name".
  2167.  
  2168. (defvar dired-ls-sorting-switches "SXU"
  2169.   "String of `ls' switches (single letters) except `t' that influence sorting.")
  2170.  
  2171. (defvar dired-sort-by-date-regexp
  2172.   (concat "^-[^" dired-ls-sorting-switches
  2173.       "]*t[^" dired-ls-sorting-switches "]*$")
  2174.   "Regexp recognized by dired to set `by date' mode.")
  2175.  
  2176. (defvar dired-sort-by-name-regexp
  2177.   (concat "^-[^t" dired-ls-sorting-switches "]+$")
  2178.   "Regexp recognized by dired to set `by name' mode.")
  2179.  
  2180. (defun dired-sort-set-modeline ()
  2181.   ;; Set modeline display according to dired-actual-switches.
  2182.   ;; Modeline display of "by name" or "by date" guarantees the user a
  2183.   ;; match with the corresponding regexps.  Non-matching switches are
  2184.   ;; shown literally.
  2185.   (setq mode-name
  2186.     (let (case-fold-search)
  2187.       (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
  2188.          "Dired by name")
  2189.         ((string-match dired-sort-by-date-regexp dired-actual-switches)
  2190.          "Dired by date")
  2191.         (t
  2192.          (concat "Dired " dired-actual-switches)))))
  2193.   (force-mode-line-update))
  2194.  
  2195. (defun dired-sort-toggle-or-edit (&optional arg)
  2196.   "Toggle between sort by date/name and refresh the dired buffer.
  2197. With a prefix argument you can edit the current listing switches instead."
  2198.   (interactive "P")
  2199.   (if arg
  2200.       (dired-sort-other
  2201.        (read-string "ls switches (must contain -l): " dired-actual-switches))
  2202.     (dired-sort-toggle)))
  2203.  
  2204. (defun dired-sort-toggle ()
  2205.   ;; Toggle between sort by date/name.  Reverts the buffer.
  2206.   (setq dired-actual-switches
  2207.     (let (case-fold-search)
  2208.       (concat
  2209.        "-l"
  2210.        (dired-replace-in-string (concat "[-lt"
  2211.                         dired-ls-sorting-switches "]")
  2212.                     ""
  2213.                     dired-actual-switches)
  2214.        (if (string-match (concat "[t" dired-ls-sorting-switches "]")
  2215.                  dired-actual-switches)
  2216.            ""
  2217.          "t"))))
  2218.   (dired-sort-set-modeline)
  2219.   (revert-buffer))
  2220.  
  2221. (defun dired-replace-in-string (regexp newtext string)
  2222.   ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
  2223.   ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
  2224.   (let ((result "") (start 0) mb me)
  2225.     (while (string-match regexp string start)
  2226.       (setq mb (match-beginning 0)
  2227.         me (match-end 0)
  2228.         result (concat result (substring string start mb) newtext)
  2229.         start me))
  2230.     (concat result (substring string start))))
  2231.  
  2232. (defun dired-sort-other (switches &optional no-revert)
  2233.   ;; Specify new ls SWITCHES for current dired buffer.  Values matching
  2234.   ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
  2235.   ;; minor mode accordingly, others appear literally in the mode line.
  2236.   ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
  2237.   (setq dired-actual-switches switches)
  2238.   (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
  2239.   (or no-revert (revert-buffer)))
  2240.  
  2241. ;; To make this file smaller, the less common commands
  2242. ;; go in a separate file.  But autoload them here
  2243. ;; to make the separation invisible.
  2244.  
  2245. (autoload 'dired-diff "dired-aux"
  2246.   "Compare file at point with file FILE using `diff'.
  2247. FILE defaults to the file at the mark.
  2248. The prompted-for file is the first file given to `diff'."
  2249.   t)
  2250.  
  2251. (autoload 'dired-backup-diff "dired-aux"
  2252.   "Diff this file with its backup file or vice versa.
  2253. Uses the latest backup, if there are several numerical backups.
  2254. If this file is a backup, diff it with its original.
  2255. The backup file is the first file given to `diff'."
  2256.   t)
  2257.  
  2258. (autoload 'dired-clean-directory "dired-aux"
  2259.   "Flag numerical backups for deletion.
  2260. Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  2261. Positive prefix arg KEEP overrides `dired-kept-versions';
  2262. Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  2263.  
  2264. To clear the flags on these files, you can use \\[dired-flag-backup-files]
  2265. with a prefix argument."
  2266.   t)
  2267.  
  2268. (autoload 'dired-do-chmod "dired-aux"
  2269.   "Change the mode of the marked (or next ARG) files.
  2270. This calls chmod, thus symbolic modes like `g+w' are allowed."
  2271.   t)
  2272.  
  2273. (autoload 'dired-do-chgrp "dired-aux"
  2274.   "Change the group of the marked (or next ARG) files."
  2275.   t)
  2276.  
  2277. (autoload 'dired-do-chown "dired-aux"
  2278.   "Change the owner of the marked (or next ARG) files."
  2279.   t)
  2280.  
  2281. (autoload 'dired-do-print "dired-aux"
  2282.   "Print the marked (or next ARG) files.
  2283. Uses the shell command coming from variables `lpr-command' and
  2284. `lpr-switches' as default."
  2285.   t)
  2286.  
  2287. (autoload 'dired-do-shell-command "dired-aux"
  2288.   "Run a shell command COMMAND on the marked files.
  2289. If no files are marked or a specific numeric prefix arg is given,
  2290. the next ARG files are used.  Just \\[universal-argument] means the current file.
  2291. The prompt mentions the file(s) or the marker, as appropriate.
  2292.  
  2293. If there is output, it goes to a separate buffer.
  2294.  
  2295. Normally the command is run on each file individually.
  2296. However, if there is a `*' in the command then it is run
  2297. just once with the entire file list substituted there.
  2298.  
  2299. No automatic redisplay of dired buffers is attempted, as there's no
  2300. telling what files the command may have changed.  Type
  2301. \\[dired-do-redisplay] to redisplay the marked files.
  2302.  
  2303. The shell command has the top level directory as working directory, so
  2304. output files usually are created there instead of in a subdir."
  2305.   t)
  2306.  
  2307. (autoload 'dired-do-kill-lines "dired-aux"
  2308.   "Kill all marked lines (not the files).
  2309. With a prefix arg, kill all lines not marked or flagged."
  2310.   t)
  2311.  
  2312. (autoload 'dired-do-compress "dired-aux"
  2313.   "Compress or uncompress marked (or next ARG) files."
  2314.   t)
  2315.  
  2316. (autoload 'dired-do-byte-compile "dired-aux"
  2317.   "Byte compile marked (or next ARG) Emacs Lisp files."
  2318.   t)
  2319.  
  2320. (autoload 'dired-do-load "dired-aux"
  2321.   "Load the marked (or next ARG) Emacs Lisp files."
  2322.   t)
  2323.  
  2324. (autoload 'dired-do-redisplay "dired-aux"
  2325.   "Redisplay all marked (or next ARG) files.
  2326. If on a subdir line, redisplay that subdirectory.  In that case,
  2327. a prefix arg lets you edit the `ls' switches used for the new listing."
  2328.   t)
  2329.  
  2330. (autoload 'dired-string-replace-match "dired-aux"
  2331.   "Replace first match of REGEXP in STRING with NEWTEXT.
  2332. If it does not match, nil is returned instead of the new string.
  2333. Optional arg LITERAL means to take NEWTEXT literally.
  2334. Optional arg GLOBAL means to replace all matches."
  2335.   t)
  2336.  
  2337. (autoload 'dired-create-directory "dired-aux"
  2338.   "Create a directory called DIRECTORY."
  2339.   t)
  2340.  
  2341. (autoload 'dired-do-copy "dired-aux"
  2342.   "Copy all marked (or next ARG) files, or copy the current file.
  2343. Thus, a zero prefix argument copies nothing.  But it toggles the
  2344. variable `dired-copy-preserve-time' (which see)."
  2345.   t)
  2346.  
  2347. (autoload 'dired-do-symlink "dired-aux"
  2348.   "Make symbolic links to current file or all marked (or next ARG) files.
  2349. When operating on just the current file, you specify the new name.
  2350. When operating on multiple or marked files, you specify a directory
  2351. and new symbolic links are made in that directory
  2352. with the same names that the files currently have."
  2353.   t)
  2354.  
  2355. (autoload 'dired-do-hardlink "dired-aux"
  2356.   "Add names (hard links) current file or all marked (or next ARG) files.
  2357. When operating on just the current file, you specify the new name.
  2358. When operating on multiple or marked files, you specify a directory
  2359. and new hard links are made in that directory
  2360. with the same names that the files currently have."
  2361.   t)
  2362.  
  2363. (autoload 'dired-do-rename "dired-aux"
  2364.   "Rename current file or all marked (or next ARG) files.
  2365. When renaming just the current file, you specify the new name.
  2366. When renaming multiple or marked files, you specify a directory."
  2367.   t)
  2368.  
  2369. (autoload 'dired-do-rename-regexp "dired-aux"
  2370.   "Rename marked files containing REGEXP to NEWNAME.
  2371. As each match is found, the user must type a character saying
  2372.   what to do with it.  For directions, type \\[help-command] at that time.
  2373. NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
  2374. REGEXP defaults to the last regexp used.
  2375. With a zero prefix arg, renaming by regexp affects the complete
  2376.   pathname - usually only the non-directory part of file names is used
  2377.   and changed."
  2378.   t)
  2379.  
  2380. (autoload 'dired-do-copy-regexp "dired-aux"
  2381.   "Copy all marked files containing REGEXP to NEWNAME.
  2382. See function `dired-rename-regexp' for more info."
  2383.   t)
  2384.  
  2385. (autoload 'dired-do-hardlink-regexp "dired-aux"
  2386.   "Hardlink all marked files containing REGEXP to NEWNAME.
  2387. See function `dired-rename-regexp' for more info."
  2388.   t)
  2389.  
  2390. (autoload 'dired-do-symlink-regexp "dired-aux"
  2391.   "Symlink all marked files containing REGEXP to NEWNAME.
  2392. See function `dired-rename-regexp' for more info."
  2393.   t)
  2394.  
  2395. (autoload 'dired-upcase "dired-aux"
  2396.   "Rename all marked (or next ARG) files to upper case."
  2397.   t)
  2398.  
  2399. (autoload 'dired-downcase "dired-aux"
  2400.   "Rename all marked (or next ARG) files to lower case."
  2401.   t)
  2402.  
  2403. (autoload 'dired-maybe-insert-subdir "dired-aux"
  2404.   "Insert this subdirectory into the same dired buffer.
  2405. If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
  2406.   else inserts it at its natural place (as `ls -lR' would have done).
  2407. With a prefix arg, you may edit the ls switches used for this listing.
  2408.   You can add `R' to the switches to expand the whole tree starting at
  2409.   this subdirectory.
  2410. This function takes some pains to conform to `ls -lR' output."
  2411.   t)
  2412.  
  2413. (autoload 'dired-next-subdir "dired-aux"
  2414.   "Go to next subdirectory, regardless of level."
  2415.   t)
  2416.  
  2417. (autoload 'dired-prev-subdir "dired-aux"
  2418.   "Go to previous subdirectory, regardless of level.
  2419. When called interactively and not on a subdir line, go to this subdir's line."
  2420.   t)
  2421.  
  2422. (autoload 'dired-goto-subdir "dired-aux"
  2423.   "Go to end of header line of DIR in this dired buffer.
  2424. Return value of point on success, otherwise return nil.
  2425. The next char is either \\n, or \\r if DIR is hidden."
  2426.   t)
  2427.  
  2428. (autoload 'dired-mark-subdir-files "dired-aux"
  2429.   "Mark all files except `.' and `..'."
  2430.   t)
  2431.  
  2432. (autoload 'dired-kill-subdir "dired-aux"
  2433.   "Remove all lines of current subdirectory.
  2434. Lower levels are unaffected."
  2435.   t)
  2436.  
  2437. (autoload 'dired-tree-up "dired-aux"
  2438.   "Go up ARG levels in the dired tree."
  2439.   t)
  2440.  
  2441. (autoload 'dired-tree-down "dired-aux"
  2442.   "Go down in the dired tree."
  2443.   t)
  2444.  
  2445. (autoload 'dired-hide-subdir "dired-aux"
  2446.   "Hide or unhide the current subdirectory and move to next directory.
  2447. Optional prefix arg is a repeat factor.
  2448. Use \\[dired-hide-all] to (un)hide all directories."
  2449.   t)
  2450.  
  2451. (autoload 'dired-hide-all "dired-aux"
  2452.   "Hide all subdirectories, leaving only their header lines.
  2453. If there is already something hidden, make everything visible again.
  2454. Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
  2455.   t)
  2456.  
  2457. (if (eq system-type 'vax-vms)
  2458.     (load "dired-vms"))
  2459.  
  2460. (provide 'dired)
  2461.  
  2462. (run-hooks 'dired-load-hook)        ; for your customizations
  2463.  
  2464. ;;; dired.el ends here
  2465.